pi-hole/advanced/Scripts/webpage.sh

306 lines
6.6 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-12-28 02:14:47 +00:00
readonly setupVars="/etc/pihole/setupVars.conf"
readonly dnsmasqconfig="/etc/dnsmasq.d/01-pihole.conf"
readonly dhcpconfig="/etc/dnsmasq.d/02-pihole-dhcp.conf"
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
}
2016-12-28 02:14:47 +00:00
add_setting() {
echo "${1}=${2}" >> "${setupVars}"
}
delete_setting() {
sed -i "/${1}/d" "${setupVars}"
}
change_setting() {
delete_setting "${1}"
add_setting "${1}" "${2}"
}
add_dnsmasq_setting() {
if [[ "${2}" != "" ]]; then
echo "${1}=${2}" >> "${dnsmasqconfig}"
else
echo "${1}" >> "${dnsmasqconfig}"
fi
}
delete_dnsmasq_setting() {
sed -i "/${1}/d" "${dnsmasqconfig}"
}
2016-11-16 20:34:43 +00:00
SetTemperatureUnit(){
2016-12-28 02:14:47 +00:00
change_setting "TEMPERATUREUNIT" "${unit}"
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
# 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
2016-12-28 02:14:47 +00:00
change_setting "WEBPASSWORD" "${hash}"
echo "New password set"
else
2016-12-28 02:14:47 +00:00
change_setting "WEBPASSWORD" ""
echo "Password removed"
fi
2016-11-16 20:34:43 +00:00
}
2016-12-28 02:14:47 +00:00
ProcessDNSSettings() {
source "${setupVars}"
delete_dnsmasq_setting "server="
add_dnsmasq_setting "server" "${PIHOLE_DNS_1}"
if [[ "${PIHOLE_DNS_2}" != "" ]]; then
add_dnsmasq_setting "server" "${PIHOLE_DNS_2}"
fi
delete_dnsmasq_setting "domain-needed"
if [[ "${DNS_FQDN_REQUIRED}" == true ]]; then
add_dnsmasq_setting "domain-needed"
fi
delete_dnsmasq_setting "bogus-priv"
if [[ "${DNS_BOGUS_PRIV}" == true ]]; then
add_dnsmasq_setting "bogus-priv"
fi
}
2016-12-11 15:54:27 +00:00
SetDNSServers(){
# Save setting to file
2016-12-28 02:14:47 +00:00
change_setting "PIHOLE_DNS_1" "${args[2]}"
2016-12-11 15:54:27 +00:00
2016-12-19 12:42:42 +00:00
if [[ "${args[3]}" != "none" ]]; then
2016-12-28 02:14:47 +00:00
change_setting "PIHOLE_DNS_2" "${args[3]}"
else
change_setting "PIHOLE_DNS_2" ""
2016-12-19 12:42:42 +00:00
fi
2016-12-14 15:09:57 +00:00
if [[ "${args[4]}" == "domain-needed" ]]; then
2016-12-28 02:14:47 +00:00
change_setting "DNS_FQDN_REQUIRED" "true"
2016-12-14 15:09:57 +00:00
else
2016-12-28 02:14:47 +00:00
change_setting "DNS_FQDN_REQUIRED" "false"
2016-12-14 15:09:57 +00:00
fi
2016-12-14 15:16:25 +00:00
2016-12-28 02:14:47 +00:00
if [[ "${args[4]}" == "bogus-priv" || "${args[5]}" == "bogus-priv" ]]; then
change_setting "DNS_BOGUS_PRIV" "true"
2016-12-14 15:16:25 +00:00
else
2016-12-28 02:14:47 +00:00
change_setting "DNS_BOGUS_PRIV" "false"
2016-12-14 15:16:25 +00:00
fi
2016-12-14 15:09:57 +00:00
2016-12-28 02:14:47 +00:00
ProcessDnsmasqSettings
# Restart dnsmasq to load new configuration
RestartDNS
2016-12-11 15:54:27 +00:00
}
SetExcludeDomains(){
2016-12-28 02:14:47 +00:00
change_setting "API_EXCLUDE_DOMAINS" "${args[2]}"
}
SetExcludeClients(){
2016-12-28 02:14:47 +00:00
change_setting "API_EXCLUDE_CLIENTS" "${args[2]}"
}
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(){
2016-12-28 02:14:47 +00:00
change_setting "API_QUERY_LOG_SHOW" "${args[2]}"
}
ProcessDHCPSettings() {
source "${setupVars}"
interface=$(grep 'PIHOLE_INTERFACE=' /etc/pihole/setupVars.conf | sed "s/.*=//")
# Use eth0 as fallback interface
if [ -z ${interface} ]; then
interface="eth0"
fi
if [[ "${DHCP_LEASETIME}" == "0" ]]; then
leasetime="infinite"
else
leasetime="${DHCP_LEASETIME}h"
fi
2016-12-28 02:14:47 +00:00
# Write settings to file
echo "###############################################################################
# DHCP SERVER CONFIG FILE AUTOMATICALLY POPULATED BY PI-HOLE WEB INTERFACE. #
# ANY CHANGES MADE TO THIS FILE WILL BE LOST ON CHANGE #
###############################################################################
dhcp-authoritative
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
domain=${PIHOLE_DOMAIN}
quiet-dhcp
quiet-dhcp6
#enable-ra
dhcp-option=option6:dns-server,[::]
2016-12-29 14:26:23 +00:00
dhcp-range=::100,::1ff,constructor:${interface},ra-names,slaac,${leasetime}
2016-12-28 20:02:48 +00:00
ra-param=*,0,0
2016-12-28 02:14:47 +00:00
" > "${dhcpconfig}"
2016-12-12 12:15:07 +00:00
}
2016-12-12 14:34:05 +00:00
EnableDHCP(){
2016-12-28 02:14:47 +00:00
change_setting "DHCP_ACTIVE" "true"
change_setting "DHCP_START" "${args[2]}"
change_setting "DHCP_END" "${args[3]}"
change_setting "DHCP_ROUTER" "${args[4]}"
change_setting "DHCP_LEASETIME" "${args[5]}"
change_setting "PIHOLE_DOMAIN" "${args[6]}"
2016-12-12 14:34:05 +00:00
2016-12-28 02:14:47 +00:00
# Remove possible old setting from file
delete_dnsmasq_setting "dhcp-"
delete_dnsmasq_setting "quiet-dhcp"
ProcessDHCPSettings
2016-12-12 14:34:05 +00:00
RestartDNS
}
DisableDHCP(){
2016-12-28 02:14:47 +00:00
change_setting "DHCP_ACTIVE" "false"
# Remove possible old setting from file
delete_dnsmasq_setting "dhcp-"
delete_dnsmasq_setting "quiet-dhcp"
2016-12-12 14:34:05 +00:00
2016-12-28 02:14:47 +00:00
rm "${dhcpconfig}"
2016-12-12 14:34:05 +00:00
RestartDNS
}
2016-12-13 13:59:52 +00:00
SetWebUILayout(){
2016-12-28 02:14:47 +00:00
change_setting "WEBUIBOXEDLAYOUT" "${args[2]}"
2016-12-13 13:59:52 +00:00
}
2016-12-16 16:33:01 +00:00
SetPrivacyMode(){
if [[ "${args[2]}" == "true" ]] ; then
2016-12-28 02:14:47 +00:00
change_setting "API_PRIVACY_MODE" "true"
2016-12-16 16:33:01 +00:00
else
2016-12-28 02:14:47 +00:00
change_setting "API_PRIVACY_MODE" "false"
2016-12-16 16:33:01 +00:00
fi
2016-12-28 02:14:47 +00:00
}
ResolutionSettings() {
2016-12-16 16:33:01 +00:00
2016-12-28 02:14:47 +00:00
typ="${args[2]}"
state="${args[3]}"
if [[ "${typ}" == "forward" ]]; then
2016-12-28 02:14:47 +00:00
change_setting "API_GET_UPSTREAM_DNS_HOSTNAME" "${state}"
elif [[ "${typ}" == "clients" ]]; then
2016-12-28 02:14:47 +00:00
change_setting "API_GET_CLIENT_HOSTNAME" "${state}"
fi
2016-12-16 16:33:01 +00:00
}
2016-12-28 16:25:14 +00:00
main() {
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;;
"reboot" ) Reboot;;
"restartdns" ) RestartDNS;;
"setquerylog" ) SetQueryLogOptions;;
"enabledhcp" ) EnableDHCP;;
"disabledhcp" ) DisableDHCP;;
"layout" ) SetWebUILayout;;
"-h" | "--help" ) helpFunc;;
"privacymode" ) SetPrivacyMode;;
"resolve" ) ResolutionSettings;;
* ) helpFunc;;
esac
shift
if [[ $# = 0 ]]; then
helpFunc
fi
2016-11-16 20:34:43 +00:00
2016-12-28 16:25:14 +00:00
}