1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-12-23 15:28:07 +00:00
pi-hole/advanced/Scripts/webpage.sh

243 lines
7.4 KiB
Bash
Raw Normal View History

2016-11-16 20:34:43 +00:00
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Web interface settings
2016-11-16 20:34:43 +00:00
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
2016-11-16 20:36:19 +00:00
args=("$@")
2016-11-16 20:34:43 +00:00
helpFunc() {
cat << EOM
2016-11-20 14:15:27 +00:00
::: Set admin options for the web interface of pihole
2016-11-16 20:34:43 +00:00
:::
2016-11-20 14:15:27 +00:00
::: Usage: pihole -a [options]
2016-11-16 20:34:43 +00:00
:::
::: Options:
2016-11-19 20:57:42 +00:00
::: -p, password Set web interface password, an empty input will remove any previously set password
2016-12-20 15:28:28 +00:00
::: -c, celsius Set Celsius temperature unit
2016-11-16 20:34:43 +00:00
::: -f, fahrenheit Set Fahrenheit temperature unit
2016-12-20 15:28:28 +00:00
::: -k, kelvin Set Kelvin temperature unit
2016-11-16 20:34:43 +00:00
::: -h, --help Show this help dialog
EOM
exit 0
2016-11-16 20:34:43 +00:00
}
SetTemperatureUnit(){
# Remove setting from file (create backup setupVars.conf.bak)
2016-11-16 21:33:28 +00:00
sed -i.bak '/TEMPERATUREUNIT/d' /etc/pihole/setupVars.conf
2016-11-16 20:34:43 +00:00
# Save setting to file
2016-12-20 11:21:08 +00:00
echo "TEMPERATUREUNIT=${unit}" >> /etc/pihole/setupVars.conf
2016-11-16 20:34:43 +00:00
}
SetWebPassword(){
2016-12-13 14:54:41 +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
2016-12-13 14:54:41 +00:00
if [ "${SUDO_USER}" == "lighttpd" ]; then
echo "Security measure: user lighttpd is not allowed to change webUI password!"
echo "Exiting"
exit 1
fi
2016-11-16 20:34:43 +00:00
# Remove password from file (create backup setupVars.conf.bak)
2016-11-16 21:33:28 +00:00
sed -i.bak '/WEBPASSWORD/d' /etc/pihole/setupVars.conf
# Set password only if there is one to be set
if (( ${#args[2]} > 0 )) ; then
# Compute password hash twice to avoid rainbow table vulnerability
hash=$(echo -n ${args[2]} | sha256sum | sed 's/\s.*$//')
hash=$(echo -n ${hash} | sha256sum | sed 's/\s.*$//')
# Save hash to file
echo "WEBPASSWORD=${hash}" >> /etc/pihole/setupVars.conf
echo "New password set"
else
echo "Password removed"
fi
2016-11-16 20:34:43 +00:00
}
2016-12-11 15:54:27 +00:00
SetDNSServers(){
# Remove setting from file (create backup setupVars.conf.bak)
2016-12-14 15:09:57 +00:00
sed -i.bak '/PIHOLE_DNS_1/d;/PIHOLE_DNS_2/d;/DNS_FQDN_REQUIRED/d;' /etc/pihole/setupVars.conf
2016-12-11 15:54:27 +00:00
# Save setting to file
echo "PIHOLE_DNS_1=${args[2]}" >> /etc/pihole/setupVars.conf
2016-12-19 12:42:42 +00:00
if [[ "${args[3]}" != "none" ]]; then
echo "PIHOLE_DNS_2=${args[3]}" >> /etc/pihole/setupVars.conf
else
echo "PIHOLE_DNS_2=" >> /etc/pihole/setupVars.conf
fi
2016-12-11 15:54:27 +00:00
# Replace within actual dnsmasq config file
sed -i '/server=/d;' /etc/dnsmasq.d/01-pihole.conf
echo "server=${args[2]}" >> /etc/dnsmasq.d/01-pihole.conf
2016-12-19 12:42:42 +00:00
if [[ "${args[3]}" != "none" ]]; then
echo "server=${args[3]}" >> /etc/dnsmasq.d/01-pihole.conf
fi
2016-12-14 15:09:57 +00:00
# Remove domain-needed entry
sed -i '/domain-needed/d;' /etc/dnsmasq.d/01-pihole.conf
# Readd it if required
if [[ "${args[4]}" == "domain-needed" ]]; then
echo "domain-needed" >> /etc/dnsmasq.d/01-pihole.conf
echo "DNS_FQDN_REQUIRED=true" >> /etc/pihole/setupVars.conf
else
2016-12-14 15:16:25 +00:00
# Leave it deleted if not wanted
2016-12-14 15:09:57 +00:00
echo "DNS_FQDN_REQUIRED=false" >> /etc/pihole/setupVars.conf
fi
2016-12-14 15:16:25 +00:00
# Remove bogus-priv entry
sed -i '/bogus-priv/d;' /etc/dnsmasq.d/01-pihole.conf
# Readd it if required
if [[ "${args[5]}" == "bogus-priv" ]]; then
echo "bogus-priv" >> /etc/dnsmasq.d/01-pihole.conf
echo "DNS_BOGUS_PRIV=true" >> /etc/pihole/setupVars.conf
else
# Leave it deleted if not wanted
echo "DNS_BOGUS_PRIV=false" >> /etc/pihole/setupVars.conf
fi
2016-12-14 15:09:57 +00:00
# Restart dnsmasq to load new configuration
RestartDNS
2016-12-11 15:54:27 +00:00
}
SetExcludeDomains(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/API_EXCLUDE_DOMAINS/d;' /etc/pihole/setupVars.conf
# Save setting to file
echo "API_EXCLUDE_DOMAINS=${args[2]}" >> /etc/pihole/setupVars.conf
}
SetExcludeClients(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/API_EXCLUDE_CLIENTS/d;' /etc/pihole/setupVars.conf
# Save setting to file
echo "API_EXCLUDE_CLIENTS=${args[2]}" >> /etc/pihole/setupVars.conf
}
2016-12-11 21:33:27 +00:00
Reboot(){
nohup bash -c "sleep 5; reboot" &> /dev/null </dev/null &
2016-12-11 21:33:27 +00:00
}
2016-12-12 09:38:21 +00:00
RestartDNS(){
if [ -x "$(command -v systemctl)" ]; then
systemctl restart dnsmasq &> /dev/null
else
service dnsmasq restart &> /dev/null
fi
}
2016-12-12 12:15:07 +00:00
SetQueryLogOptions(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/API_QUERY_LOG_SHOW/d;' /etc/pihole/setupVars.conf
# Save setting to file
echo "API_QUERY_LOG_SHOW=${args[2]}" >> /etc/pihole/setupVars.conf
}
2016-12-12 14:34:05 +00:00
EnableDHCP(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/DHCP_/d;' /etc/pihole/setupVars.conf
echo "DHCP_ACTIVE=true" >> /etc/pihole/setupVars.conf
echo "DHCP_START=${args[2]}" >> /etc/pihole/setupVars.conf
echo "DHCP_END=${args[3]}" >> /etc/pihole/setupVars.conf
echo "DHCP_ROUTER=${args[4]}" >> /etc/pihole/setupVars.conf
# Remove setting from file
sed -i '/dhcp-/d;/quiet-dhcp/d;' /etc/dnsmasq.d/01-pihole.conf
2016-12-12 14:34:05 +00:00
# Save setting to file
echo "dhcp-range=${args[2]},${args[3]},infinite" >> /etc/dnsmasq.d/01-pihole.conf
echo "dhcp-option=option:router,${args[4]}" >> /etc/dnsmasq.d/01-pihole.conf
# Changes the behaviour from strict RFC compliance so that DHCP requests on unknown leases from unknown hosts are not ignored. This allows new hosts to get a lease without a tedious timeout under all circumstances. It also allows dnsmasq to rebuild its lease database without each client needing to reacquire a lease, if the database is lost.
echo "dhcp-authoritative" >> /etc/dnsmasq.d/01-pihole.conf
# Use the specified file to store DHCP lease information
echo "dhcp-leasefile=/etc/pihole/dhcp.leases" >> /etc/dnsmasq.d/01-pihole.conf
# Suppress logging of the routine operation of these protocols. Errors and problems will still be logged, though.
echo "quiet-dhcp" >> /etc/dnsmasq.d/01-pihole.conf
echo "quiet-dhcp6" >> /etc/dnsmasq.d/01-pihole.conf
2016-12-12 14:34:05 +00:00
RestartDNS
}
DisableDHCP(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/DHCP_ACTIVE/d;' /etc/pihole/setupVars.conf
echo "DHCP_ACTIVE=false" >> /etc/pihole/setupVars.conf
# Remove setting from file
sed -i '/dhcp-/d;/quiet-dhcp/d;' /etc/dnsmasq.d/01-pihole.conf
2016-12-12 14:34:05 +00:00
RestartDNS
}
2016-12-13 13:59:52 +00:00
SetWebUILayout(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/WEBUIBOXEDLAYOUT/d;' /etc/pihole/setupVars.conf
echo "WEBUIBOXEDLAYOUT=${args[2]}" >> /etc/pihole/setupVars.conf
}
2016-12-14 13:24:09 +00:00
SetDNSDomainName(){
# Remove setting from file (create backup setupVars.conf.bak)
sed -i.bak '/PIHOLE_DOMAIN/d;' /etc/pihole/setupVars.conf
# Save setting to file
echo "PIHOLE_DOMAIN=${args[2]}" >> /etc/pihole/setupVars.conf
# Replace within actual dnsmasq config file
sed -i '/domain=/d;' /etc/dnsmasq.d/01-pihole.conf
echo "domain=${args[2]}" >> /etc/dnsmasq.d/01-pihole.conf
# Restart dnsmasq to load new configuration
RestartDNS
}
case "${args[1]}" in
"-p" | "password" ) SetWebPassword;;
"-c" | "celsius" ) unit="C"; SetTemperatureUnit;;
"-f" | "fahrenheit" ) unit="F"; SetTemperatureUnit;;
2016-12-20 11:21:08 +00:00
"-k" | "kelvin" ) unit="K"; SetTemperatureUnit;;
2016-12-14 13:24:09 +00:00
"setdns" ) SetDNSServers;;
"setexcludedomains" ) SetExcludeDomains;;
"setexcludeclients" ) SetExcludeClients;;
"reboot" ) Reboot;;
"restartdns" ) RestartDNS;;
"setquerylog" ) SetQueryLogOptions;;
"enabledhcp" ) EnableDHCP;;
"disabledhcp" ) DisableDHCP;;
"layout" ) SetWebUILayout;;
"-h" | "--help" ) helpFunc;;
"domainname" ) SetDNSDomainName;;
* ) helpFunc;;
esac
2016-11-16 20:34:43 +00:00
shift
if [[ $# = 0 ]]; then
helpFunc
fi