From c35ed6805159e3df319d0bb663cc145d5987a8af Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Mon, 27 Feb 2023 01:46:12 -0300 Subject: [PATCH 1/3] Allow `pihole -q` matching ABP subdomains Signed-off-by: RD WebDesign --- advanced/Scripts/query.sh | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/advanced/Scripts/query.sh b/advanced/Scripts/query.sh index 463b0901..71309b56 100755 --- a/advanced/Scripts/query.sh +++ b/advanced/Scripts/query.sh @@ -102,6 +102,16 @@ scanDatabaseTable() { table="${2}" list_type="${3:-}" + # Create search string for ABP entries + local abpentry="${domain}" searchstr + + searchstr="'||${abpentry}^'" + while [ "${abpentry}" != "${abpentry/./}" ] + do + abpentry=$(echo "${abpentry}" | cut -f 2- -d '.') + searchstr=$(echo "$searchstr, '||${abpentry}^'") + done + # 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 # behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched @@ -109,12 +119,12 @@ scanDatabaseTable() { if [[ "${table}" == "gravity" ]]; then case "${exact}" in "exact" ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE domain = '${domain}'";; - * ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";; + * ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE (domain IN (${searchstr}) OR domain LIKE '%${domain//_/\\_}%' ESCAPE '\\')";; esac else case "${exact}" in "exact" ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND domain = '${domain}'";; - * ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";; + * ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND (domain IN (${searchstr}) OR domain LIKE '%${domain//_/\\_}%' ESCAPE '\\')";; esac fi From 20f8c6af3c3b10fa4be6bd23365ecd7b89c93cea Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Tue, 28 Feb 2023 15:55:02 -0300 Subject: [PATCH 2/3] Search for ABP entries only if they exist in gravity.db and use `abp_domains` property. Signed-off-by: RD WebDesign --- advanced/Scripts/query.sh | 40 +++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/advanced/Scripts/query.sh b/advanced/Scripts/query.sh index 71309b56..c6a932b7 100755 --- a/advanced/Scripts/query.sh +++ b/advanced/Scripts/query.sh @@ -102,29 +102,45 @@ scanDatabaseTable() { table="${2}" list_type="${3:-}" - # Create search string for ABP entries - local abpentry="${domain}" searchstr - - searchstr="'||${abpentry}^'" - while [ "${abpentry}" != "${abpentry/./}" ] - do - abpentry=$(echo "${abpentry}" | cut -f 2- -d '.') - searchstr=$(echo "$searchstr, '||${abpentry}^'") - done - # 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 # behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched # as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores. if [[ "${table}" == "gravity" ]]; then + local abpquerystr, abpfound, abpentry, searchstr + + # Are there ABP entries on gravity? + # Return 1 if abp_domain=1 or Zero if abp_domain=0 or not set + abpquerystr="SELECT EXISTS (SELECT 1 FROM info WHERE property='abp_domains' and value='1')" + abpfound="$(pihole-FTL sqlite3 "${gravityDBfile}" "${abpquerystr}")" 2> /dev/null + + # Create search string for ABP entries only if needed + if [ "${abpfound}" -eq 1 ]; then + abpentry="${domain}" + + searchstr="'||${abpentry}^'" + + # While a dot is found ... + while [ "${abpentry}" != "${abpentry/./}" ] + do + # ... remove text before the dot (including the dot) and append the result to $searchstr + abpentry=$(echo "${abpentry}" | cut -f 2- -d '.') + searchstr="$searchstr, '||${abpentry}^'" + done + + # The final search string will look like: + # "domain IN ('||sub2.sub1.domain.com^', '||sub1.domain.com^', '||domain.com^', '||com^') OR" + searchstr="domain IN (${searchstr}) OR " + fi + case "${exact}" in "exact" ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE domain = '${domain}'";; - * ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE (domain IN (${searchstr}) OR domain LIKE '%${domain//_/\\_}%' ESCAPE '\\')";; + * ) querystr="SELECT gravity.domain,adlist.address,adlist.enabled FROM gravity LEFT JOIN adlist ON adlist.id = gravity.adlist_id WHERE ${searchstr} domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";; esac else case "${exact}" in "exact" ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND domain = '${domain}'";; - * ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND (domain IN (${searchstr}) OR domain LIKE '%${domain//_/\\_}%' ESCAPE '\\')";; + * ) querystr="SELECT domain,enabled FROM domainlist WHERE type = '${list_type}' AND domain LIKE '%${domain//_/\\_}%' ESCAPE '\\'";; esac fi From 6b919f3a2e36f724e8dd2a8f3b4e518d28de20a1 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Tue, 21 Mar 2023 12:27:20 -0300 Subject: [PATCH 3/3] Removing unnecessary commas Signed-off-by: RD WebDesign --- advanced/Scripts/query.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced/Scripts/query.sh b/advanced/Scripts/query.sh index c6a932b7..604ac2ed 100755 --- a/advanced/Scripts/query.sh +++ b/advanced/Scripts/query.sh @@ -107,7 +107,7 @@ scanDatabaseTable() { # behavior. The "ESCAPE '\'" clause specifies that an underscore preceded by an '\' should be matched # as a literal underscore character. We pretreat the $domain variable accordingly to escape underscores. if [[ "${table}" == "gravity" ]]; then - local abpquerystr, abpfound, abpentry, searchstr + local abpquerystr abpfound abpentry searchstr # Are there ABP entries on gravity? # Return 1 if abp_domain=1 or Zero if abp_domain=0 or not set