2016-11-16 20:34:43 +00:00
|
|
|
#!/usr/bin/env bash
|
2017-09-14 14:24:29 +00:00
|
|
|
# shellcheck disable=SC1090
|
2022-03-16 20:46:15 +00:00
|
|
|
# shellcheck disable=SC2154
|
|
|
|
|
2017-09-14 14:24:29 +00:00
|
|
|
|
2016-11-16 20:34:43 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2016-11-16 21:13:47 +00:00
|
|
|
# Web interface settings
|
2016-11-16 20:34:43 +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.
|
|
|
|
|
2016-12-28 02:14:47 +00:00
|
|
|
readonly dnsmasqconfig="/etc/dnsmasq.d/01-pihole.conf"
|
|
|
|
readonly dhcpconfig="/etc/dnsmasq.d/02-pihole-dhcp.conf"
|
2018-04-04 11:12:39 +00:00
|
|
|
readonly FTLconf="/etc/pihole/pihole-FTL.conf"
|
2017-01-25 09:33:25 +00:00
|
|
|
# 03 -> wildcards
|
|
|
|
readonly dhcpstaticconfig="/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
|
2019-10-27 00:07:08 +00:00
|
|
|
readonly dnscustomfile="/etc/pihole/custom.list"
|
2020-05-12 19:02:29 +00:00
|
|
|
readonly dnscustomcnamefile="/etc/dnsmasq.d/05-pihole-custom-cname.conf"
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2019-04-28 19:39:06 +00:00
|
|
|
readonly gravityDBfile="/etc/pihole/gravity.db"
|
|
|
|
|
2020-06-13 19:57:57 +00:00
|
|
|
|
2023-05-07 11:21:23 +00:00
|
|
|
readonly setupVars="/etc/pihole/setupVars.conf"
|
|
|
|
readonly PI_HOLE_BIN_DIR="/usr/local/bin"
|
|
|
|
|
|
|
|
# Root of the web server
|
|
|
|
readonly webroot="/var/www/html"
|
|
|
|
|
|
|
|
# Source utils script
|
2022-04-12 16:13:50 +00:00
|
|
|
utilsfile="/opt/pihole/utils.sh"
|
2022-03-16 17:42:01 +00:00
|
|
|
source "${utilsfile}"
|
|
|
|
|
2017-06-21 11:49:05 +00:00
|
|
|
coltable="/opt/pihole/COL_TABLE"
|
|
|
|
if [[ -f ${coltable} ]]; then
|
2018-07-20 20:57:15 +00:00
|
|
|
source ${coltable}
|
2017-06-21 11:49:05 +00:00
|
|
|
fi
|
|
|
|
|
2016-11-16 20:34:43 +00:00
|
|
|
helpFunc() {
|
2018-07-20 20:57:15 +00:00
|
|
|
echo "Usage: pihole -a [options]
|
2017-05-14 01:11:44 +00:00
|
|
|
Example: pihole -a -p password
|
|
|
|
Set options for the Admin Console
|
|
|
|
|
|
|
|
Options:
|
2022-02-04 20:19:09 +00:00
|
|
|
-p, password Set Admin Console password
|
|
|
|
-c, celsius Set Celsius as preferred temperature unit
|
|
|
|
-f, fahrenheit Set Fahrenheit as preferred temperature unit
|
|
|
|
-k, kelvin Set Kelvin as preferred temperature unit
|
|
|
|
-h, --help Show this help dialog
|
|
|
|
-i, interface Specify dnsmasq's interface listening behavior
|
|
|
|
-l, privacylevel Set privacy level (0 = lowest, 3 = highest)
|
|
|
|
-t, teleporter Backup configuration as an archive
|
|
|
|
-t, teleporter myname.tar.gz Backup configuration to archive with name myname.tar.gz as specified"
|
2018-07-20 20:57:15 +00:00
|
|
|
exit 0
|
2016-11-16 20:34:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 02:14:47 +00:00
|
|
|
add_setting() {
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "${1}" "${2}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete_setting() {
|
2022-04-04 21:02:26 +00:00
|
|
|
removeKey "${setupVars}" "${1}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
change_setting() {
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "${1}" "${2}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 11:12:39 +00:00
|
|
|
addFTLsetting() {
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "${FTLconf}" "${1}" "${2}"
|
2018-04-04 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteFTLsetting() {
|
2022-04-04 21:02:26 +00:00
|
|
|
removeKey "${FTLconf}" "${1}"
|
2018-04-04 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changeFTLsetting() {
|
2023-01-14 23:15:40 +00:00
|
|
|
pihole-FTL --config "${1}" "${2}"
|
2018-04-04 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 02:14:47 +00:00
|
|
|
add_dnsmasq_setting() {
|
2022-03-16 20:46:15 +00:00
|
|
|
addOrEditKeyValPair "${dnsmasqconfig}" "${1}" "${2}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete_dnsmasq_setting() {
|
2022-04-04 21:02:26 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "${1}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetTemperatureUnit() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "TEMPERATUREUNIT" "${unit}"
|
2018-07-20 20:57:15 +00:00
|
|
|
echo -e " ${TICK} Set temperature unit to ${unit}"
|
2016-11-16 20:34:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
HashPassword() {
|
2018-07-20 20:57:15 +00:00
|
|
|
# Compute password hash twice to avoid rainbow table vulnerability
|
2019-08-14 21:28:13 +00:00
|
|
|
return=$(echo -n "${1}" | sha256sum | sed 's/\s.*$//')
|
|
|
|
return=$(echo -n "${return}" | sha256sum | sed 's/\s.*$//')
|
|
|
|
echo "${return}"
|
2017-05-02 21:24:37 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 11:21:23 +00:00
|
|
|
# Check an IP address to see if it is a valid one
|
|
|
|
valid_ip() {
|
|
|
|
# Local, named variables
|
|
|
|
local ip=${1}
|
|
|
|
local stat=1
|
|
|
|
|
|
|
|
# Regex matching one IPv4 component, i.e. an integer from 0 to 255.
|
|
|
|
# See https://tools.ietf.org/html/rfc1340
|
|
|
|
local ipv4elem="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)";
|
|
|
|
# Regex matching an optional port (starting with '#') range of 1-65536
|
|
|
|
local portelem="(#(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}|0))?";
|
|
|
|
# Build a full IPv4 regex from the above subexpressions
|
|
|
|
local regex="^${ipv4elem}\\.${ipv4elem}\\.${ipv4elem}\\.${ipv4elem}${portelem}$"
|
|
|
|
|
|
|
|
# Evaluate the regex, and return the result
|
|
|
|
[[ $ip =~ ${regex} ]]
|
|
|
|
|
|
|
|
stat=$?
|
|
|
|
return "${stat}"
|
|
|
|
}
|
|
|
|
|
|
|
|
valid_ip6() {
|
|
|
|
local ip=${1}
|
|
|
|
local stat=1
|
|
|
|
|
|
|
|
# Regex matching one IPv6 element, i.e. a hex value from 0000 to FFFF
|
|
|
|
local ipv6elem="[0-9a-fA-F]{1,4}"
|
|
|
|
# Regex matching an IPv6 CIDR, i.e. 1 to 128
|
|
|
|
local v6cidr="(\\/([1-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){0,1}"
|
|
|
|
# Regex matching an optional port (starting with '#') range of 1-65536
|
|
|
|
local portelem="(#(6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3}|0))?";
|
|
|
|
# Build a full IPv6 regex from the above subexpressions
|
|
|
|
local regex="^(((${ipv6elem}))*((:${ipv6elem}))*::((${ipv6elem}))*((:${ipv6elem}))*|((${ipv6elem}))((:${ipv6elem})){7})${v6cidr}${portelem}$"
|
|
|
|
|
|
|
|
# Evaluate the regex, and return the result
|
|
|
|
[[ ${ip} =~ ${regex} ]]
|
|
|
|
|
|
|
|
stat=$?
|
|
|
|
return "${stat}"
|
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetWebPassword() {
|
2018-07-20 20:57:15 +00:00
|
|
|
if [ "${SUDO_USER}" == "www-data" ]; then
|
|
|
|
echo "Security measure: user www-data is not allowed to change webUI password!"
|
|
|
|
echo "Exiting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${SUDO_USER}" == "lighttpd" ]; then
|
|
|
|
echo "Security measure: user lighttpd is not allowed to change webUI password!"
|
|
|
|
echo "Exiting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if (( ${#args[2]} > 0 )) ; then
|
|
|
|
readonly PASSWORD="${args[2]}"
|
|
|
|
readonly CONFIRM="${PASSWORD}"
|
|
|
|
else
|
|
|
|
# Prevents a bug if the user presses Ctrl+C and it continues to hide the text typed.
|
|
|
|
# So we reset the terminal via stty if the user does press Ctrl+C
|
|
|
|
trap '{ echo -e "\nNo password will be set" ; stty sane ; exit 1; }' INT
|
2018-10-26 18:17:49 +00:00
|
|
|
read -s -r -p "Enter New Password (Blank for no password): " PASSWORD
|
2018-07-20 20:57:15 +00:00
|
|
|
echo ""
|
2017-05-04 09:43:48 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
if [ "${PASSWORD}" == "" ]; then
|
2023-01-14 23:15:40 +00:00
|
|
|
setFTLConfigValue "api.pwhash" ""
|
2021-11-25 06:41:40 +00:00
|
|
|
echo -e " ${TICK} Password Removed"
|
|
|
|
exit 0
|
|
|
|
fi
|
2017-05-04 09:43:48 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
read -s -r -p "Confirm Password: " CONFIRM
|
|
|
|
echo ""
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2017-03-27 17:37:19 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [ "${PASSWORD}" == "${CONFIRM}" ] ; then
|
2018-10-21 00:08:22 +00:00
|
|
|
# We do not wrap this in brackets, otherwise BASH will expand any appropriate syntax
|
|
|
|
hash=$(HashPassword "$PASSWORD")
|
2018-07-20 20:57:15 +00:00
|
|
|
# Save hash to file
|
2023-01-14 23:15:40 +00:00
|
|
|
setFTLConfigValue "api.pwhash" "${hash}"
|
2018-07-20 20:57:15 +00:00
|
|
|
echo -e " ${TICK} New password set"
|
|
|
|
else
|
|
|
|
echo -e " ${CROSS} Passwords don't match. Your password has not been changed"
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-11-16 20:34:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 02:14:47 +00:00
|
|
|
ProcessDNSSettings() {
|
2018-07-20 20:57:15 +00:00
|
|
|
source "${setupVars}"
|
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "server"
|
2018-07-20 20:57:15 +00:00
|
|
|
|
|
|
|
COUNTER=1
|
2019-08-14 21:28:13 +00:00
|
|
|
while true ; do
|
2018-07-20 20:57:15 +00:00
|
|
|
var=PIHOLE_DNS_${COUNTER}
|
|
|
|
if [ -z "${!var}" ]; then
|
|
|
|
break;
|
|
|
|
fi
|
2022-04-17 12:39:55 +00:00
|
|
|
addKey "${dnsmasqconfig}" "server=${!var}"
|
2019-08-14 21:28:13 +00:00
|
|
|
(( COUNTER++ ))
|
2018-07-20 20:57:15 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# The option LOCAL_DNS_PORT is deprecated
|
|
|
|
# We apply it once more, and then convert it into the current format
|
2019-08-14 21:28:13 +00:00
|
|
|
if [ -n "${LOCAL_DNS_PORT}" ]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${dnsmasqconfig}" "server" "127.0.0.1#${LOCAL_DNS_PORT}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "PIHOLE_DNS_${COUNTER}" "127.0.0.1#${LOCAL_DNS_PORT}"
|
|
|
|
removeKey "${setupVars}" "LOCAL_DNS_PORT"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2018-04-18 15:12:20 +00:00
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "domain-needed"
|
|
|
|
removeKey "${dnsmasqconfig}" "expand-hosts"
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${DNS_FQDN_REQUIRED}" == true ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "domain-needed"
|
|
|
|
addKey "${dnsmasqconfig}" "expand-hosts"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "bogus-priv"
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${DNS_BOGUS_PRIV}" == true ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "bogus-priv"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "dnssec"
|
|
|
|
removeKey "${dnsmasqconfig}" "trust-anchor"
|
2017-01-12 15:02:41 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${DNSSEC}" == true ]]; then
|
|
|
|
echo "dnssec
|
2017-12-18 21:14:28 +00:00
|
|
|
trust-anchor=.,20326,8,2,E06D44B80B8F1D39A95C0B0D7C65D08458E880409BBC683457104237C7F8EC8D
|
2017-01-12 15:02:41 +00:00
|
|
|
" >> "${dnsmasqconfig}"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2017-01-12 15:02:41 +00:00
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "host-record"
|
2017-02-22 13:43:07 +00:00
|
|
|
|
2019-08-14 21:28:13 +00:00
|
|
|
if [ -n "${HOSTRECORD}" ]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${dnsmasqconfig}" "host-record" "${HOSTRECORD}"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2017-02-22 13:43:07 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
# Setup interface listening behavior of dnsmasq
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "interface"
|
|
|
|
removeKey "${dnsmasqconfig}" "local-service"
|
|
|
|
removeKey "${dnsmasqconfig}" "except-interface"
|
|
|
|
removeKey "${dnsmasqconfig}" "bind-interfaces"
|
2017-03-01 09:46:20 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${DNSMASQ_LISTENING}" == "all" ]]; then
|
|
|
|
# Listen on all interfaces, permit all origins
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${dnsmasqconfig}" "except-interface" "nonexisting"
|
2018-07-20 20:57:15 +00:00
|
|
|
elif [[ "${DNSMASQ_LISTENING}" == "local" ]]; then
|
|
|
|
# Listen only on all interfaces, but only local subnets
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "local-service"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2021-12-20 20:36:19 +00:00
|
|
|
# Options "bind" and "single"
|
2018-07-20 20:57:15 +00:00
|
|
|
# Listen only on one interface
|
|
|
|
# Use eth0 as fallback interface if interface is missing in setupVars.conf
|
|
|
|
if [ -z "${PIHOLE_INTERFACE}" ]; then
|
|
|
|
PIHOLE_INTERFACE="eth0"
|
|
|
|
fi
|
2018-01-07 13:34:02 +00:00
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${dnsmasqconfig}" "interface" "${PIHOLE_INTERFACE}"
|
2021-12-20 20:36:19 +00:00
|
|
|
|
|
|
|
if [[ "${DNSMASQ_LISTENING}" == "bind" ]]; then
|
|
|
|
# Really bind to interface
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "bind-interfaces"
|
2021-12-20 20:36:19 +00:00
|
|
|
fi
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2017-03-01 09:46:20 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${CONDITIONAL_FORWARDING}" == true ]]; then
|
2020-04-08 13:35:07 +00:00
|
|
|
# Convert legacy "conditional forwarding" to rev-server configuration
|
2020-11-30 21:18:25 +00:00
|
|
|
# Remove any existing REV_SERVER settings
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${setupVars}" "REV_SERVER"
|
|
|
|
removeKey "${setupVars}" "REV_SERVER_DOMAIN"
|
|
|
|
removeKey "${setupVars}" "REV_SERVER_TARGET"
|
|
|
|
removeKey "${setupVars}" "REV_SERVER_CIDR"
|
2020-11-30 18:58:23 +00:00
|
|
|
|
2020-04-08 13:35:07 +00:00
|
|
|
REV_SERVER=true
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER" "true"
|
2020-04-08 13:35:07 +00:00
|
|
|
|
|
|
|
REV_SERVER_DOMAIN="${CONDITIONAL_FORWARDING_DOMAIN}"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_DOMAIN" "${REV_SERVER_DOMAIN}"
|
2020-04-08 13:35:07 +00:00
|
|
|
|
|
|
|
REV_SERVER_TARGET="${CONDITIONAL_FORWARDING_IP}"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_TARGET" "${REV_SERVER_TARGET}"
|
2020-04-08 13:35:07 +00:00
|
|
|
|
2020-11-30 22:30:59 +00:00
|
|
|
#Convert CONDITIONAL_FORWARDING_REVERSE if necessary e.g:
|
2020-11-30 21:18:25 +00:00
|
|
|
# 1.1.168.192.in-addr.arpa to 192.168.1.1/32
|
|
|
|
# 1.168.192.in-addr.arpa to 192.168.1.0/24
|
|
|
|
# 168.192.in-addr.arpa to 192.168.0.0/16
|
|
|
|
# 192.in-addr.arpa to 192.0.0.0/8
|
2020-12-01 17:27:21 +00:00
|
|
|
if [[ "${CONDITIONAL_FORWARDING_REVERSE}" == *"in-addr.arpa" ]];then
|
2021-02-11 18:45:32 +00:00
|
|
|
arrRev=("${CONDITIONAL_FORWARDING_REVERSE//./ }")
|
|
|
|
case ${#arrRev[@]} in
|
2020-11-30 18:58:23 +00:00
|
|
|
6 ) REV_SERVER_CIDR="${arrRev[3]}.${arrRev[2]}.${arrRev[1]}.${arrRev[0]}/32";;
|
|
|
|
5 ) REV_SERVER_CIDR="${arrRev[2]}.${arrRev[1]}.${arrRev[0]}.0/24";;
|
|
|
|
4 ) REV_SERVER_CIDR="${arrRev[1]}.${arrRev[0]}.0.0/16";;
|
2021-02-11 18:45:32 +00:00
|
|
|
3 ) REV_SERVER_CIDR="${arrRev[0]}.0.0.0/8";;
|
2020-11-30 18:58:23 +00:00
|
|
|
esac
|
2020-11-30 21:29:54 +00:00
|
|
|
else
|
2021-11-25 06:41:40 +00:00
|
|
|
# Set REV_SERVER_CIDR to whatever value it was set to
|
|
|
|
REV_SERVER_CIDR="${CONDITIONAL_FORWARDING_REVERSE}"
|
2020-11-30 18:58:23 +00:00
|
|
|
fi
|
2021-02-11 18:45:32 +00:00
|
|
|
|
2020-11-30 21:18:25 +00:00
|
|
|
# If REV_SERVER_CIDR is not converted by the above, then use the REV_SERVER_TARGET variable to derive it
|
2020-09-25 13:26:43 +00:00
|
|
|
if [ -z "${REV_SERVER_CIDR}" ]; then
|
|
|
|
# Convert existing input to /24 subnet (preserves legacy behavior)
|
|
|
|
# This sed converts "192.168.1.2" to "192.168.1.0/24"
|
|
|
|
# shellcheck disable=2001
|
|
|
|
REV_SERVER_CIDR="$(sed "s+\\.[0-9]*$+\\.0/24+" <<< "${REV_SERVER_TARGET}")"
|
|
|
|
fi
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_CIDR" "${REV_SERVER_CIDR}"
|
2020-09-25 13:26:43 +00:00
|
|
|
|
2020-04-08 13:35:07 +00:00
|
|
|
# Remove obsolete settings from setupVars.conf
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${setupVars}" "CONDITIONAL_FORWARDING"
|
|
|
|
removeKey "${setupVars}" "CONDITIONAL_FORWARDING_REVERSE"
|
|
|
|
removeKey "${setupVars}" "CONDITIONAL_FORWARDING_DOMAIN"
|
|
|
|
removeKey "${setupVars}" "CONDITIONAL_FORWARDING_IP"
|
2020-04-08 13:35:07 +00:00
|
|
|
fi
|
|
|
|
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "rev-server"
|
2021-08-20 14:26:17 +00:00
|
|
|
|
2020-04-08 13:35:07 +00:00
|
|
|
if [[ "${REV_SERVER}" == true ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "rev-server=${REV_SERVER_CIDR},${REV_SERVER_TARGET}"
|
2020-04-08 13:35:07 +00:00
|
|
|
if [ -n "${REV_SERVER_DOMAIN}" ]; then
|
2021-08-20 12:04:50 +00:00
|
|
|
# Forward local domain names to the CF target, too
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "server=/${REV_SERVER_DOMAIN}/${REV_SERVER_TARGET}"
|
2020-04-08 13:35:07 +00:00
|
|
|
fi
|
2021-09-09 18:28:33 +00:00
|
|
|
|
|
|
|
if [[ "${DNS_FQDN_REQUIRED}" != true ]]; then
|
|
|
|
# Forward unqualified names to the CF target only when the "never
|
|
|
|
# forward non-FQDN" option is unticked
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "server=//${REV_SERVER_TARGET}"
|
2021-09-09 18:28:33 +00:00
|
|
|
fi
|
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2019-09-07 21:11:20 +00:00
|
|
|
|
2020-12-01 08:16:50 +00:00
|
|
|
# We need to process DHCP settings here as well to account for possible
|
|
|
|
# changes in the non-FQDN forwarding. This cannot be done in 01-pihole.conf
|
|
|
|
# as we don't want to delete all local=/.../ lines so it's much safer to
|
|
|
|
# simply rewrite the entire corresponding config file (which is what the
|
2022-07-02 13:00:17 +00:00
|
|
|
# DHCP settings subroutine is doing)
|
2020-12-01 08:16:50 +00:00
|
|
|
ProcessDHCPSettings
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetDNSServers() {
|
2018-07-20 20:57:15 +00:00
|
|
|
# Save setting to file
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${setupVars}" "PIHOLE_DNS"
|
2018-07-20 20:57:15 +00:00
|
|
|
IFS=',' read -r -a array <<< "${args[2]}"
|
|
|
|
for index in "${!array[@]}"
|
|
|
|
do
|
2020-06-04 07:32:15 +00:00
|
|
|
# Replace possible "\#" by "#". This fixes AdminLTE#1427
|
2020-06-13 19:57:57 +00:00
|
|
|
local ip
|
|
|
|
ip="${array[index]//\\#/#}"
|
|
|
|
|
2020-06-23 19:38:18 +00:00
|
|
|
if valid_ip "${ip}" || valid_ip6 "${ip}" ; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "PIHOLE_DNS_$((index+1))" "${ip}"
|
2020-06-13 19:57:57 +00:00
|
|
|
else
|
|
|
|
echo -e " ${CROSS} Invalid IP has been passed"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-07-20 20:57:15 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ "${args[3]}" == "domain-needed" ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNS_FQDN_REQUIRED" "true"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNS_FQDN_REQUIRED" "false"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${args[4]}" == "bogus-priv" ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNS_BOGUS_PRIV" "true"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNS_BOGUS_PRIV" "false"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "${args[5]}" == "dnssec" ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSSEC" "true"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSSEC" "false"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
|
|
|
|
2020-04-08 13:35:07 +00:00
|
|
|
if [[ "${args[6]}" == "rev-server" ]]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER" "true"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_CIDR" "${args[7]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_TARGET" "${args[8]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER_DOMAIN" "${args[9]}"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "REV_SERVER" "false"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
ProcessDNSSettings
|
|
|
|
|
|
|
|
# Restart dnsmasq to load new configuration
|
|
|
|
RestartDNS
|
2016-12-11 15:54:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetExcludeDomains() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "API_EXCLUDE_DOMAINS" "${args[2]}"
|
2016-12-11 18:30:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetExcludeClients() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "API_EXCLUDE_CLIENTS" "${args[2]}"
|
2016-12-11 18:30:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 17:33:06 +00:00
|
|
|
Poweroff(){
|
2018-07-20 20:57:15 +00:00
|
|
|
nohup bash -c "sleep 5; poweroff" &> /dev/null </dev/null &
|
2017-04-11 17:04:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
Reboot() {
|
2018-07-20 20:57:15 +00:00
|
|
|
nohup bash -c "sleep 5; reboot" &> /dev/null </dev/null &
|
2016-12-11 21:33:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
RestartDNS() {
|
2019-08-24 11:33:32 +00:00
|
|
|
"${PI_HOLE_BIN_DIR}"/pihole restartdns
|
2016-12-12 09:38:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetQueryLogOptions() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "API_QUERY_LOG_SHOW" "${args[2]}"
|
2016-12-28 02:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessDHCPSettings() {
|
2018-07-20 20:57:15 +00:00
|
|
|
source "${setupVars}"
|
2017-01-03 12:34:01 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${DHCP_ACTIVE}" == "true" ]]; then
|
2021-11-25 06:41:40 +00:00
|
|
|
interface="${PIHOLE_INTERFACE}"
|
2017-01-02 09:50:59 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
# Use eth0 as fallback interface
|
|
|
|
if [ -z ${interface} ]; then
|
|
|
|
interface="eth0"
|
|
|
|
fi
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
if [[ "${PIHOLE_DOMAIN}" == "" ]]; then
|
|
|
|
PIHOLE_DOMAIN="lan"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "PIHOLE_DOMAIN" "${PIHOLE_DOMAIN}"
|
2021-11-25 06:41:40 +00:00
|
|
|
fi
|
2017-01-02 10:08:54 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
if [[ "${DHCP_LEASETIME}" == "0" ]]; then
|
|
|
|
leasetime="infinite"
|
|
|
|
elif [[ "${DHCP_LEASETIME}" == "" ]]; then
|
2022-10-01 16:11:54 +00:00
|
|
|
leasetime="24h"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_LEASETIME" "24"
|
2021-11-25 06:41:40 +00:00
|
|
|
else
|
|
|
|
leasetime="${DHCP_LEASETIME}h"
|
|
|
|
fi
|
2016-12-29 14:19:44 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
# Write settings to file
|
|
|
|
echo "###############################################################################
|
2016-12-28 02:14:47 +00:00
|
|
|
# DHCP SERVER CONFIG FILE AUTOMATICALLY POPULATED BY PI-HOLE WEB INTERFACE. #
|
|
|
|
# ANY CHANGES MADE TO THIS FILE WILL BE LOST ON CHANGE #
|
|
|
|
###############################################################################
|
|
|
|
dhcp-authoritative
|
2016-12-29 14:19:44 +00:00
|
|
|
dhcp-range=${DHCP_START},${DHCP_END},${leasetime}
|
2016-12-28 02:14:47 +00:00
|
|
|
dhcp-option=option:router,${DHCP_ROUTER}
|
|
|
|
dhcp-leasefile=/etc/pihole/dhcp.leases
|
2016-12-30 16:31:57 +00:00
|
|
|
#quiet-dhcp
|
2017-01-03 13:24:33 +00:00
|
|
|
" > "${dhcpconfig}"
|
2021-11-25 06:41:40 +00:00
|
|
|
chmod 644 "${dhcpconfig}"
|
|
|
|
|
|
|
|
if [[ "${PIHOLE_DOMAIN}" != "none" ]]; then
|
|
|
|
echo "domain=${PIHOLE_DOMAIN}" >> "${dhcpconfig}"
|
|
|
|
|
|
|
|
# When there is a Pi-hole domain set and "Never forward non-FQDNs" is
|
|
|
|
# ticked, we add `local=/domain/` to tell FTL that this domain is purely
|
|
|
|
# local and FTL may answer queries from /etc/hosts or DHCP but should
|
|
|
|
# never forward queries on that domain to any upstream servers
|
|
|
|
if [[ "${DNS_FQDN_REQUIRED}" == true ]]; then
|
|
|
|
echo "local=/${PIHOLE_DOMAIN}/" >> "${dhcpconfig}"
|
|
|
|
fi
|
2020-10-10 09:23:39 +00:00
|
|
|
fi
|
2017-01-18 11:22:03 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
# Sourced from setupVars
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
if [[ "${DHCP_rapid_commit}" == "true" ]]; then
|
|
|
|
echo "dhcp-rapid-commit" >> "${dhcpconfig}"
|
|
|
|
fi
|
2018-11-25 07:39:44 +00:00
|
|
|
|
2021-11-25 06:41:40 +00:00
|
|
|
if [[ "${DHCP_IPv6}" == "true" ]]; then
|
|
|
|
echo "#quiet-dhcp6
|
2016-12-28 02:14:47 +00:00
|
|
|
#enable-ra
|
|
|
|
dhcp-option=option6:dns-server,[::]
|
2022-05-11 18:57:42 +00:00
|
|
|
dhcp-range=::,constructor:${interface},ra-names,ra-stateless,64
|
2022-06-02 19:24:01 +00:00
|
|
|
|
2017-01-03 13:24:33 +00:00
|
|
|
" >> "${dhcpconfig}"
|
2021-11-25 06:41:40 +00:00
|
|
|
fi
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
|
|
|
if [[ -f "${dhcpconfig}" ]]; then
|
|
|
|
rm "${dhcpconfig}" &> /dev/null
|
|
|
|
fi
|
|
|
|
fi
|
2016-12-12 12:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
EnableDHCP() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_ACTIVE" "true"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_START" "${args[2]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_END" "${args[3]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_ROUTER" "${args[4]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_LEASETIME" "${args[5]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "PIHOLE_DOMAIN" "${args[6]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_IPv6" "${args[7]}"
|
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_rapid_commit" "${args[8]}"
|
2016-12-12 14:34:05 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
# Remove possible old setting from file
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "dhcp-"
|
|
|
|
removeKey "${dnsmasqconfig}" "quiet-dhcp"
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2019-05-30 14:41:37 +00:00
|
|
|
# If a DHCP client claims that its name is "wpad", ignore that.
|
|
|
|
# This fixes a security hole. see CERT Vulnerability VU#598349
|
|
|
|
# We also ignore "localhost" as Windows behaves strangely if a
|
|
|
|
# device claims this host name
|
2022-04-15 08:50:40 +00:00
|
|
|
addKey "${dnsmasqconfig}" "dhcp-name-match=set:hostname-ignore,wpad
|
2019-05-30 14:41:37 +00:00
|
|
|
dhcp-name-match=set:hostname-ignore,localhost
|
|
|
|
dhcp-ignore-names=tag:hostname-ignore"
|
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
ProcessDHCPSettings
|
2016-12-12 14:34:05 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
RestartDNS
|
2016-12-12 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
DisableDHCP() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DHCP_ACTIVE" "false"
|
2016-12-28 02:14:47 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
# Remove possible old setting from file
|
2022-04-15 08:50:40 +00:00
|
|
|
removeKey "${dnsmasqconfig}" "dhcp-"
|
|
|
|
removeKey "${dnsmasqconfig}" "quiet-dhcp"
|
2016-12-12 14:34:05 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
ProcessDHCPSettings
|
2016-12-12 14:34:05 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
RestartDNS
|
2016-12-12 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetWebUILayout() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "WEBUIBOXEDLAYOUT" "${args[2]}"
|
2016-12-13 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 23:22:08 +00:00
|
|
|
SetWebUITheme() {
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "WEBTHEME" "${args[2]}"
|
2020-04-29 23:22:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 20:48:10 +00:00
|
|
|
CheckUrl(){
|
2020-12-06 23:23:04 +00:00
|
|
|
local regex check_url
|
2020-03-31 20:48:10 +00:00
|
|
|
# Check for characters NOT allowed in URLs
|
2020-12-06 23:23:04 +00:00
|
|
|
regex="[^a-zA-Z0-9:/?&%=~._()-;]"
|
|
|
|
|
|
|
|
# this will remove first @ that is after schema and before domain
|
|
|
|
# \1 is optional schema, \2 is userinfo
|
|
|
|
check_url="$( sed -re 's#([^:/]*://)?([^/]+)@#\1\2#' <<< "$1" )"
|
|
|
|
|
|
|
|
if [[ "${check_url}" =~ ${regex} ]]; then
|
2020-03-31 20:48:10 +00:00
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-03-31 18:35:52 +00:00
|
|
|
CustomizeAdLists() {
|
2019-04-28 19:39:06 +00:00
|
|
|
local address
|
|
|
|
address="${args[3]}"
|
2019-10-14 21:59:58 +00:00
|
|
|
local comment
|
|
|
|
comment="${args[4]}"
|
2018-07-20 20:57:15 +00:00
|
|
|
|
2020-03-31 20:48:10 +00:00
|
|
|
if CheckUrl "${address}"; then
|
|
|
|
if [[ "${args[2]}" == "enable" ]]; then
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "UPDATE adlist SET enabled = 1 WHERE address = '${address}'"
|
2020-03-31 20:48:10 +00:00
|
|
|
elif [[ "${args[2]}" == "disable" ]]; then
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "UPDATE adlist SET enabled = 0 WHERE address = '${address}'"
|
2020-03-31 20:48:10 +00:00
|
|
|
elif [[ "${args[2]}" == "add" ]]; then
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "INSERT OR IGNORE INTO adlist (address, comment) VALUES ('${address}', '${comment}')"
|
2020-03-31 20:48:10 +00:00
|
|
|
elif [[ "${args[2]}" == "del" ]]; then
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "DELETE FROM adlist WHERE address = '${address}'"
|
2020-03-31 20:48:10 +00:00
|
|
|
else
|
|
|
|
echo "Not permitted"
|
|
|
|
return 1
|
|
|
|
fi
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
2020-03-31 20:48:10 +00:00
|
|
|
echo "Invalid Url"
|
2018-07-20 20:57:15 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2017-03-31 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 09:33:25 +00:00
|
|
|
AddDHCPStaticAddress() {
|
2018-07-20 20:57:15 +00:00
|
|
|
mac="${args[2]}"
|
|
|
|
ip="${args[3]}"
|
|
|
|
host="${args[4]}"
|
|
|
|
|
|
|
|
if [[ "${ip}" == "noip" ]]; then
|
|
|
|
# Static host name
|
|
|
|
echo "dhcp-host=${mac},${host}" >> "${dhcpstaticconfig}"
|
|
|
|
elif [[ "${host}" == "nohost" ]]; then
|
|
|
|
# Static IP
|
|
|
|
echo "dhcp-host=${mac},${ip}" >> "${dhcpstaticconfig}"
|
|
|
|
else
|
|
|
|
# Full info given
|
|
|
|
echo "dhcp-host=${mac},${ip},${host}" >> "${dhcpstaticconfig}"
|
|
|
|
fi
|
2017-01-25 09:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 09:35:03 +00:00
|
|
|
RemoveDHCPStaticAddress() {
|
2018-07-20 20:57:15 +00:00
|
|
|
mac="${args[2]}"
|
2021-03-19 18:39:43 +00:00
|
|
|
if [[ "$mac" =~ ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ ]]; then
|
|
|
|
sed -i "/dhcp-host=${mac}.*/d" "${dhcpstaticconfig}"
|
|
|
|
else
|
|
|
|
echo " ${CROSS} Invalid Mac Passed!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2017-01-25 09:35:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
SetListeningMode() {
|
2018-07-20 20:57:15 +00:00
|
|
|
source "${setupVars}"
|
2017-07-07 01:44:40 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "$3" == "-h" ]] || [[ "$3" == "--help" ]]; then
|
|
|
|
echo "Usage: pihole -a -i [interface]
|
2017-05-14 01:11:44 +00:00
|
|
|
Example: 'pihole -a -i local'
|
|
|
|
Specify dnsmasq's network interface listening behavior
|
|
|
|
|
|
|
|
Interfaces:
|
2021-12-20 20:36:19 +00:00
|
|
|
local Only respond to queries from devices that
|
|
|
|
are at most one hop away (local devices)
|
|
|
|
single Respond only on interface ${PIHOLE_INTERFACE}
|
|
|
|
bind Bind only on interface ${PIHOLE_INTERFACE}
|
2017-05-14 01:11:44 +00:00
|
|
|
all Listen on all interfaces, permit all origins"
|
2018-07-20 20:57:15 +00:00
|
|
|
exit 0
|
2021-11-25 06:41:40 +00:00
|
|
|
fi
|
2017-07-07 01:44:40 +00:00
|
|
|
|
2018-07-20 20:57:15 +00:00
|
|
|
if [[ "${args[2]}" == "all" ]]; then
|
2019-10-16 02:29:55 +00:00
|
|
|
echo -e " ${INFO} Listening on all interfaces, permitting all origins. Please use a firewall!"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSMASQ_LISTENING" "all"
|
2018-07-20 20:57:15 +00:00
|
|
|
elif [[ "${args[2]}" == "local" ]]; then
|
2019-10-16 02:29:55 +00:00
|
|
|
echo -e " ${INFO} Listening on all interfaces, permitting origins from one hop away (LAN)"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSMASQ_LISTENING" "local"
|
2021-12-20 20:36:19 +00:00
|
|
|
elif [[ "${args[2]}" == "bind" ]]; then
|
|
|
|
echo -e " ${INFO} Binding on interface ${PIHOLE_INTERFACE}"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSMASQ_LISTENING" "bind"
|
2018-07-20 20:57:15 +00:00
|
|
|
else
|
|
|
|
echo -e " ${INFO} Listening only on interface ${PIHOLE_INTERFACE}"
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${setupVars}" "DNSMASQ_LISTENING" "single"
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Don't restart DNS server yet because other settings
|
|
|
|
# will be applied afterwards if "-web" is set
|
|
|
|
if [[ "${args[3]}" != "-web" ]]; then
|
|
|
|
ProcessDNSSettings
|
|
|
|
# Restart dnsmasq to load new configuration
|
|
|
|
RestartDNS
|
|
|
|
fi
|
2017-03-01 09:46:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-14 01:11:44 +00:00
|
|
|
Teleporter() {
|
2022-01-28 15:26:57 +00:00
|
|
|
local filename
|
|
|
|
filename="${args[2]}"
|
|
|
|
if [[ -z "${filename}" ]]; then
|
|
|
|
local datetimestamp
|
|
|
|
local host
|
|
|
|
datetimestamp=$(date "+%Y-%m-%d_%H-%M-%S")
|
|
|
|
host=$(hostname)
|
|
|
|
host="${host//./_}"
|
|
|
|
filename="pi-hole-${host:-noname}-teleporter_${datetimestamp}.tar.gz"
|
|
|
|
fi
|
2022-05-31 12:11:39 +00:00
|
|
|
php "${webroot}/admin/scripts/pi-hole/php/teleporter.php" > "${filename}"
|
2017-03-05 13:08:44 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 07:32:41 +00:00
|
|
|
checkDomain()
|
|
|
|
{
|
|
|
|
local domain validDomain
|
|
|
|
# Convert to lowercase
|
|
|
|
domain="${1,,}"
|
2023-05-05 21:05:56 +00:00
|
|
|
validDomain=$(grep -P "^((-|_)*[a-z0-9]((-|_)*[a-z0-9)*(-|_)*)(\\.(-|_)*([a-z0-9]((-|_)*[a-z0-9])*))*$" <<< "${domain}") # Valid chars check
|
2019-07-06 07:32:41 +00:00
|
|
|
validDomain=$(grep -P "^[^\\.]{1,63}(\\.[^\\.]{1,63})*$" <<< "${validDomain}") # Length of each label
|
|
|
|
echo "${validDomain}"
|
|
|
|
}
|
|
|
|
|
2022-10-21 10:25:23 +00:00
|
|
|
escapeDots()
|
|
|
|
{
|
2022-10-22 20:37:05 +00:00
|
|
|
# SC suggest bashism ${variable//search/replace}
|
2022-10-21 12:31:16 +00:00
|
|
|
# shellcheck disable=SC2001
|
2022-10-22 20:37:05 +00:00
|
|
|
escaped=$(echo "$1" | sed 's/\./\\./g')
|
2022-10-21 10:25:23 +00:00
|
|
|
echo "${escaped}"
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:22:55 +00:00
|
|
|
addAudit()
|
2017-06-27 08:53:24 +00:00
|
|
|
{
|
2018-08-10 16:22:55 +00:00
|
|
|
shift # skip "-a"
|
|
|
|
shift # skip "audit"
|
2019-07-06 07:32:41 +00:00
|
|
|
local domains validDomain
|
2019-07-07 19:21:56 +00:00
|
|
|
domains=""
|
2019-07-05 12:10:33 +00:00
|
|
|
for domain in "$@"
|
2018-08-10 16:22:55 +00:00
|
|
|
do
|
2021-11-25 06:41:40 +00:00
|
|
|
# Check domain to be added. Only continue if it is valid
|
|
|
|
validDomain="$(checkDomain "${domain}")"
|
|
|
|
if [[ -n "${validDomain}" ]]; then
|
|
|
|
# Put comma in between domains when there is
|
|
|
|
# more than one domains to be added
|
|
|
|
# SQL INSERT allows adding multiple rows at once using the format
|
|
|
|
## INSERT INTO table (domain) VALUES ('abc.de'),('fgh.ij'),('klm.no'),('pqr.st');
|
|
|
|
if [[ -n "${domains}" ]]; then
|
|
|
|
domains="${domains},"
|
|
|
|
fi
|
|
|
|
domains="${domains}('${domain}')"
|
2019-07-07 19:21:56 +00:00
|
|
|
fi
|
2018-08-10 16:22:55 +00:00
|
|
|
done
|
2019-07-09 09:41:44 +00:00
|
|
|
# Insert only the domain here. The date_added field will be
|
|
|
|
# filled with its default value (date_added = current timestamp)
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "INSERT INTO domain_audit (domain) VALUES ${domains};"
|
2018-08-10 16:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clearAudit()
|
|
|
|
{
|
2022-01-30 09:42:13 +00:00
|
|
|
pihole-FTL sqlite3 "${gravityDBfile}" "DELETE FROM domain_audit;"
|
2017-06-27 08:53:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 11:12:39 +00:00
|
|
|
SetPrivacyLevel() {
|
2020-10-13 22:53:23 +00:00
|
|
|
# Set privacy level. Minimum is 0, maximum is 3
|
|
|
|
if [ "${args[2]}" -ge 0 ] && [ "${args[2]}" -le 3 ]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${FTLconf}" "PRIVACYLEVEL" "${args[2]}"
|
2020-05-20 22:47:40 +00:00
|
|
|
pihole restartdns reload-lists
|
2018-07-20 20:57:15 +00:00
|
|
|
fi
|
2018-04-04 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 00:07:08 +00:00
|
|
|
AddCustomDNSAddress() {
|
2019-11-26 23:27:57 +00:00
|
|
|
echo -e " ${TICK} Adding custom DNS entry..."
|
|
|
|
|
2019-10-27 00:07:08 +00:00
|
|
|
ip="${args[2]}"
|
|
|
|
host="${args[3]}"
|
2021-10-12 17:49:36 +00:00
|
|
|
reload="${args[4]}"
|
2021-10-12 16:15:56 +00:00
|
|
|
|
2021-10-19 19:34:16 +00:00
|
|
|
validHost="$(checkDomain "${host}")"
|
|
|
|
if [[ -n "${validHost}" ]]; then
|
|
|
|
if valid_ip "${ip}" || valid_ip6 "${ip}" ; then
|
|
|
|
echo "${ip} ${validHost}" >> "${dnscustomfile}"
|
|
|
|
else
|
|
|
|
echo -e " ${CROSS} Invalid IP has been passed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo " ${CROSS} Invalid Domain passed!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-11-26 23:27:57 +00:00
|
|
|
|
2021-10-12 17:49:36 +00:00
|
|
|
# Restart dnsmasq to load new custom DNS entries only if $reload not false
|
|
|
|
if [[ ! $reload == "false" ]]; then
|
2021-11-25 06:41:40 +00:00
|
|
|
RestartDNS
|
2021-10-12 17:49:36 +00:00
|
|
|
fi
|
2019-10-27 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveCustomDNSAddress() {
|
2019-11-26 23:27:57 +00:00
|
|
|
echo -e " ${TICK} Removing custom DNS entry..."
|
|
|
|
|
2019-10-28 12:21:05 +00:00
|
|
|
ip="${args[2]}"
|
|
|
|
host="${args[3]}"
|
2021-10-12 17:49:36 +00:00
|
|
|
reload="${args[4]}"
|
2021-03-19 18:39:43 +00:00
|
|
|
|
2021-10-19 19:34:16 +00:00
|
|
|
validHost="$(checkDomain "${host}")"
|
|
|
|
if [[ -n "${validHost}" ]]; then
|
|
|
|
if valid_ip "${ip}" || valid_ip6 "${ip}" ; then
|
2022-10-21 10:25:23 +00:00
|
|
|
validHost=$(escapeDots "${validHost}")
|
2022-02-04 21:16:02 +00:00
|
|
|
sed -i "/^${ip} ${validHost}$/Id" "${dnscustomfile}"
|
2021-10-19 19:34:16 +00:00
|
|
|
else
|
|
|
|
echo -e " ${CROSS} Invalid IP has been passed"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-11-25 06:41:40 +00:00
|
|
|
else
|
|
|
|
echo " ${CROSS} Invalid Domain passed!"
|
|
|
|
exit 1
|
2021-10-17 18:51:59 +00:00
|
|
|
fi
|
2019-11-26 23:27:57 +00:00
|
|
|
|
2021-10-12 17:49:36 +00:00
|
|
|
# Restart dnsmasq to load new custom DNS entries only if reload is not false
|
|
|
|
if [[ ! $reload == "false" ]]; then
|
2021-11-25 06:41:40 +00:00
|
|
|
RestartDNS
|
2021-10-12 17:49:36 +00:00
|
|
|
fi
|
2019-10-27 00:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:02:29 +00:00
|
|
|
AddCustomCNAMERecord() {
|
|
|
|
echo -e " ${TICK} Adding custom CNAME record..."
|
|
|
|
|
|
|
|
domain="${args[2]}"
|
|
|
|
target="${args[3]}"
|
2021-10-12 17:49:36 +00:00
|
|
|
reload="${args[4]}"
|
2021-03-19 18:39:43 +00:00
|
|
|
|
2021-10-12 16:15:56 +00:00
|
|
|
validDomain="$(checkDomain "${domain}")"
|
|
|
|
if [[ -n "${validDomain}" ]]; then
|
|
|
|
validTarget="$(checkDomain "${target}")"
|
|
|
|
if [[ -n "${validTarget}" ]]; then
|
2022-10-20 04:53:13 +00:00
|
|
|
if [ "${validDomain}" = "${validTarget}" ]; then
|
2022-10-29 13:09:14 +00:00
|
|
|
echo " ${CROSS} Domain and target are the same. This would cause a DNS loop."
|
2022-10-20 04:53:13 +00:00
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "cname=${validDomain},${validTarget}" >> "${dnscustomcnamefile}"
|
|
|
|
fi
|
2021-10-12 16:15:56 +00:00
|
|
|
else
|
2021-11-25 06:41:40 +00:00
|
|
|
echo " ${CROSS} Invalid Target Passed!"
|
|
|
|
exit 1
|
2021-10-12 16:15:56 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo " ${CROSS} Invalid Domain passed!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-10-12 17:49:36 +00:00
|
|
|
# Restart dnsmasq to load new custom CNAME records only if reload is not false
|
|
|
|
if [[ ! $reload == "false" ]]; then
|
2021-11-25 06:41:40 +00:00
|
|
|
RestartDNS
|
2021-10-12 17:49:36 +00:00
|
|
|
fi
|
2020-05-12 19:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveCustomCNAMERecord() {
|
|
|
|
echo -e " ${TICK} Removing custom CNAME record..."
|
|
|
|
|
|
|
|
domain="${args[2]}"
|
|
|
|
target="${args[3]}"
|
2021-10-12 17:49:36 +00:00
|
|
|
reload="${args[4]}"
|
2021-03-19 18:39:43 +00:00
|
|
|
|
2021-10-17 18:51:59 +00:00
|
|
|
validDomain="$(checkDomain "${domain}")"
|
|
|
|
if [[ -n "${validDomain}" ]]; then
|
|
|
|
validTarget="$(checkDomain "${target}")"
|
|
|
|
if [[ -n "${validTarget}" ]]; then
|
2022-10-21 10:25:23 +00:00
|
|
|
validDomain=$(escapeDots "${validDomain}")
|
|
|
|
validTarget=$(escapeDots "${validTarget}")
|
|
|
|
sed -i "/^cname=${validDomain},${validTarget}$/Id" "${dnscustomcnamefile}"
|
2021-10-17 18:51:59 +00:00
|
|
|
else
|
2021-11-25 06:41:40 +00:00
|
|
|
echo " ${CROSS} Invalid Target Passed!"
|
|
|
|
exit 1
|
2021-10-17 18:51:59 +00:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo " ${CROSS} Invalid Domain passed!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-05-12 19:02:29 +00:00
|
|
|
|
2021-10-12 17:49:36 +00:00
|
|
|
# Restart dnsmasq to update removed custom CNAME records only if $reload not false
|
|
|
|
if [[ ! $reload == "false" ]]; then
|
2021-11-25 06:41:40 +00:00
|
|
|
RestartDNS
|
2021-10-12 17:49:36 +00:00
|
|
|
fi
|
2020-05-12 19:02:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 08:30:53 +00:00
|
|
|
SetRateLimit() {
|
|
|
|
local rate_limit_count rate_limit_interval reload
|
|
|
|
rate_limit_count="${args[2]}"
|
|
|
|
rate_limit_interval="${args[3]}"
|
|
|
|
reload="${args[4]}"
|
|
|
|
|
|
|
|
# Set rate-limit setting inf valid
|
|
|
|
if [ "${rate_limit_count}" -ge 0 ] && [ "${rate_limit_interval}" -ge 0 ]; then
|
2022-04-15 08:50:40 +00:00
|
|
|
addOrEditKeyValPair "${FTLconf}" "RATE_LIMIT" "${rate_limit_count}/${rate_limit_interval}"
|
2022-02-19 08:30:53 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Restart FTL to update rate-limit settings only if $reload not false
|
|
|
|
if [[ ! $reload == "false" ]]; then
|
|
|
|
RestartDNS
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-12-28 16:25:14 +00:00
|
|
|
main() {
|
2018-07-20 20:57:15 +00:00
|
|
|
args=("$@")
|
|
|
|
|
|
|
|
case "${args[1]}" in
|
|
|
|
"-p" | "password" ) SetWebPassword;;
|
|
|
|
"-c" | "celsius" ) unit="C"; SetTemperatureUnit;;
|
|
|
|
"-f" | "fahrenheit" ) unit="F"; SetTemperatureUnit;;
|
|
|
|
"-k" | "kelvin" ) unit="K"; SetTemperatureUnit;;
|
|
|
|
"setdns" ) SetDNSServers;;
|
|
|
|
"setexcludedomains" ) SetExcludeDomains;;
|
|
|
|
"setexcludeclients" ) SetExcludeClients;;
|
|
|
|
"poweroff" ) Poweroff;;
|
|
|
|
"reboot" ) Reboot;;
|
|
|
|
"restartdns" ) RestartDNS;;
|
|
|
|
"setquerylog" ) SetQueryLogOptions;;
|
|
|
|
"enabledhcp" ) EnableDHCP;;
|
|
|
|
"disabledhcp" ) DisableDHCP;;
|
|
|
|
"layout" ) SetWebUILayout;;
|
2020-04-29 23:22:08 +00:00
|
|
|
"theme" ) SetWebUITheme;;
|
2018-07-20 20:57:15 +00:00
|
|
|
"-h" | "--help" ) helpFunc;;
|
|
|
|
"addstaticdhcp" ) AddDHCPStaticAddress;;
|
|
|
|
"removestaticdhcp" ) RemoveDHCPStaticAddress;;
|
|
|
|
"-i" | "interface" ) SetListeningMode "$@";;
|
|
|
|
"-t" | "teleporter" ) Teleporter;;
|
|
|
|
"adlist" ) CustomizeAdLists;;
|
2018-08-10 16:22:55 +00:00
|
|
|
"audit" ) addAudit "$@";;
|
|
|
|
"clearaudit" ) clearAudit;;
|
2018-07-20 20:57:15 +00:00
|
|
|
"-l" | "privacylevel" ) SetPrivacyLevel;;
|
2019-10-27 00:07:08 +00:00
|
|
|
"addcustomdns" ) AddCustomDNSAddress;;
|
|
|
|
"removecustomdns" ) RemoveCustomDNSAddress;;
|
2020-05-12 19:02:29 +00:00
|
|
|
"addcustomcname" ) AddCustomCNAMERecord;;
|
|
|
|
"removecustomcname" ) RemoveCustomCNAMERecord;;
|
2022-02-19 08:30:53 +00:00
|
|
|
"ratelimit" ) SetRateLimit;;
|
2018-07-20 20:57:15 +00:00
|
|
|
* ) helpFunc;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
shift
|
|
|
|
|
|
|
|
if [[ $# = 0 ]]; then
|
|
|
|
helpFunc
|
|
|
|
fi
|
2016-12-28 16:25:14 +00:00
|
|
|
}
|