2017-03-04 06:13:11 +01:00
#!/bin/bash
2022-07-19 19:22:49 +02:00
# If user has set QUERY_LOGGING Env Var, copy it out to _OVERRIDE,
# else it will get overridden itself when we source basic-install.sh
[ -n " ${ QUERY_LOGGING } " ] && export QUERY_LOGGING_OVERRIDE = " ${ QUERY_LOGGING } "
# Legacy Env Vars preserved for backwards compatibility - convert them to FTLCONF_ equivalents
2022-08-22 22:42:29 +02:00
[ -n " ${ ServerIP } " ] && echo "ServerIP is deprecated. Converting to FTLCONF_LOCAL_IPV4" && export "FTLCONF_LOCAL_IPV4" = " $ServerIP "
[ -n " ${ ServerIPv6 } " ] && echo "ServerIPv6 is deprecated. Converting to FTLCONF_LOCAL_IPV6" && export "FTLCONF_LOCAL_IPV6" = " $ServerIPv6 "
# Previously used FTLCONF_ equivalent has since been deprecated, also convert this one
[ -n " ${ FTLCONF_REPLY_ADDR4 } " ] && echo "FTLCONF_REPLY_ADDR4 is deprecated. Converting to FTLCONF_LOCAL_IPV4" && export "FTLCONF_LOCAL_IPV4" = " $FTLCONF_REPLY_ADDR4 "
[ -n " ${ FTLCONF_REPLY_ADDR6 } " ] && echo "FTLCONF_REPLY_ADDR6 is deprecated. Converting to FTLCONF_LOCAL_IPV6" && export "FTLCONF_LOCAL_IPV6" = " $FTLCONF_REPLY_ADDR6 "
2022-07-19 19:22:49 +02:00
2022-07-12 00:50:05 +02:00
# Some of the bash_functions use utilities from Pi-hole's utils.sh
# shellcheck disable=SC2154
# shellcheck source=/dev/null
. /opt/pihole/utils.sh
export setupVars = "/etc/pihole/setupVars.conf"
export FTLconf = "/etc/pihole/pihole-FTL.conf"
export dnsmasqconfig = "/etc/dnsmasq.d/01-pihole.conf"
export adlistFile = "/etc/pihole/adlists.list"
change_setting( ) {
addOrEditKeyValPair " ${ setupVars } " " ${ 1 } " " ${ 2 } "
}
changeFTLsetting( ) {
addOrEditKeyValPair " ${ FTLconf } " " ${ 1 } " " ${ 2 } "
}
2017-01-08 17:42:39 +01:00
2022-08-24 22:27:38 +02:00
fix_capabilities( ) {
# Testing on Docker 20.10.14 with no caps set shows the following caps available to the container:
# Current: cap_chown,cap_dac_override,cap_fowner,cap_fsetid,cap_kill,cap_setgid,cap_setuid,cap_setpcap,cap_net_bind_service,cap_net_raw,cap_sys_chroot,cap_mknod,cap_audit_write,cap_setfcap=ep
# FTL can also use CAP_NET_ADMIN and CAP_SYS_NICE. If we try to set them when they haven't been explicitly enabled, FTL will not start. Test for them first:
/sbin/capsh --has-p= cap_chown 2>/dev/null && CAP_STR += ',CAP_CHOWN'
/sbin/capsh --has-p= cap_net_bind_service 2>/dev/null && CAP_STR += ',CAP_NET_BIND_SERVICE'
/sbin/capsh --has-p= cap_net_raw 2>/dev/null && CAP_STR += ',CAP_NET_RAW'
/sbin/capsh --has-p= cap_net_admin 2>/dev/null && CAP_STR += ',CAP_NET_ADMIN' || DHCP_READY = 'false'
/sbin/capsh --has-p= cap_sys_nice 2>/dev/null && CAP_STR += ',CAP_SYS_NICE'
if [ [ ${ CAP_STR } ] ] ; then
# We have the (some of) the above caps available to us - apply them to pihole-FTL
setcap ${ CAP_STR : 1 } +ep " $( which pihole-FTL) " || ret = $?
if [ [ $DHCP_READY = = false ] ] && [ [ $DHCP_ACTIVE = = true ] ] ; then
# DHCP is requested but NET_ADMIN is not available.
echo "ERROR: DHCP requested but NET_ADMIN is not available. DHCP will not be started."
echo " Please add cap_net_admin to the container's capabilities or disable DHCP."
DHCP_ACTIVE = 'false'
change_setting "DHCP_ACTIVE" "false"
fi
if [ [ $ret -ne 0 && " ${ DNSMASQ_USER :- pihole } " != "root" ] ] ; then
echo "ERROR: Unable to set capabilities for pihole-FTL. Cannot run as non-root."
echo " If you are seeing this error, please set the environment variable 'DNSMASQ_USER' to the value 'root'"
exit 1
fi
else
echo "WARNING: Unable to set capabilities for pihole-FTL."
echo " Please ensure that the container has the required capabilities."
exit 1
fi
}
2022-07-12 00:50:05 +02:00
# shellcheck disable=SC2034
ensure_basic_configuration( ) {
2021-01-24 14:54:19 +01:00
# Set Debian webserver variables for installConfigs
LIGHTTPD_USER = "www-data"
LIGHTTPD_GROUP = "www-data"
LIGHTTPD_CFG = "lighttpd.conf.debian"
2018-08-03 05:49:55 +02:00
installConfigs
2022-07-10 17:54:22 +02:00
installLogrotate || true #installLogRotate can return 2 or 3, but we are still OK to continue in that case
2022-05-30 08:00:16 +02:00
2022-03-31 20:49:35 +02:00
if [ ! -f " ${ setupVars } " ] ; then
2022-04-04 21:59:07 +02:00
install -m 644 /dev/null " ${ setupVars } "
2022-03-31 20:49:35 +02:00
echo " Creating empty ${ setupVars } file. "
2022-07-12 00:59:12 +02:00
# The following setting needs to exist else the web interface version won't show in pihole -v
2022-07-12 00:50:05 +02:00
change_setting "INSTALL_WEB_INTERFACE" "true"
2022-03-31 20:49:35 +02:00
fi
2022-05-30 08:00:16 +02:00
2018-08-03 05:49:55 +02:00
set +e
mkdir -p /var/run/pihole /var/log/pihole
2022-07-12 00:50:05 +02:00
touch /var/log/pihole/FTL.log /var/log/pihole/pihole.log
2022-05-30 08:00:16 +02:00
2018-08-03 05:49:55 +02:00
chown pihole:root /etc/lighttpd
2022-05-30 08:00:16 +02:00
2022-04-22 18:41:29 +02:00
# In case of `pihole` UID being changed, re-chown the pihole scripts and pihole command
2022-01-20 18:10:19 +01:00
chown -R pihole:root " ${ PI_HOLE_INSTALL_DIR } "
chown pihole:root " ${ PI_HOLE_BIN_DIR } /pihole "
2022-05-30 08:00:16 +02:00
2018-08-03 05:49:55 +02:00
set -e
2018-08-06 01:20:55 +02:00
# Update version numbers
pihole updatechecker
2018-08-03 05:49:55 +02:00
# Re-write all of the setupVars to ensure required ones are present (like QUERY_LOGGING)
2020-05-04 23:15:35 +02:00
2018-08-03 05:49:55 +02:00
# If the setup variable file exists,
if [ [ -e " ${ setupVars } " ] ] ; then
2020-05-04 23:15:35 +02:00
cp -f " ${ setupVars } " " ${ setupVars } .update.bak "
2018-08-03 05:49:55 +02:00
fi
2022-07-10 17:54:22 +02:00
2022-07-10 17:54:22 +02:00
# Remove any existing macvendor.db and replace it with a symblink to the one moved to the root directory (see install.sh)
if [ [ -f "/etc/pihole/macvendor.db" ] ] ; then
rm /etc/pihole/macvendor.db
2022-07-10 17:54:22 +02:00
fi
2022-07-10 17:54:22 +02:00
ln -s /macvendor.db /etc/pihole/macvendor.db
2022-07-12 00:50:05 +02:00
# When fresh empty directory volumes are used then we need to create this file
if [ ! -f /etc/dnsmasq.d/01-pihole.conf ] ; then
cp /etc/.pihole/advanced/01-pihole.conf /etc/dnsmasq.d/
fi ;
2022-07-12 16:18:33 +02:00
# setup_or_skip_gravity
2017-01-21 22:02:08 +01:00
}
2016-10-07 02:46:20 +02:00
validate_env( ) {
2022-08-22 22:42:29 +02:00
# Optional FTLCONF_LOCAL_IPV4 is a valid IP
2018-01-27 04:32:39 +01:00
# nc won't throw any text based errors when it times out connecting to a valid IP, otherwise it complains about the DNS name being garbage
# if nc doesn't behave as we expect on a valid IP the routing table should be able to look it up and return a 0 retcode
2022-08-22 22:42:29 +02:00
if [ [ " $( nc -4 -w1 -z " $FTLCONF_LOCAL_IPV4 " 53 2>& 1) " != "" ] ] && ! ip route get " $FTLCONF_LOCAL_IPV4 " > /dev/null ; then
echo " ERROR: FTLCONF_LOCAL_IPV4 Environment variable ( $FTLCONF_LOCAL_IPV4 ) doesn't appear to be a valid IPv4 address "
2017-05-19 07:36:53 +02:00
exit 1
fi
# Optional IPv6 is a valid address
2022-08-22 22:42:29 +02:00
if [ [ -n " $FTLCONF_LOCAL_IPV6 " ] ] ; then
if [ [ " $FTLCONF_LOCAL_IPV6 " = = 'kernel' ] ] ; then
2022-04-22 18:41:29 +02:00
echo "ERROR: You passed in IPv6 with a value of 'kernel', this maybe because you do not have IPv6 enabled on your network"
2022-08-22 22:42:29 +02:00
unset FTLCONF_LOCAL_IPV6
2018-01-05 05:30:40 +01:00
exit 1
2017-05-19 07:36:53 +02:00
fi
2022-08-22 22:42:29 +02:00
if [ [ " $( nc -6 -w1 -z " $FTLCONF_LOCAL_IPV6 " 53 2>& 1) " != "" ] ] && ! ip route get " $FTLCONF_LOCAL_IPV6 " > /dev/null ; then
echo " ERROR: FTLCONF_LOCAL_IPV6 Environment variable ( $FTLCONF_LOCAL_IPV6 ) doesn't appear to be a valid IPv6 address "
echo " TIP: If your server is not IPv6 enabled just remove '-e FTLCONF_LOCAL_IPV6' from your docker container"
2017-05-19 07:36:53 +02:00
exit 1
fi
fi ;
2016-10-07 02:46:20 +02:00
}
2022-07-12 00:50:05 +02:00
setup_FTL_User( ) {
2019-02-04 01:37:03 +01:00
# Run DNSMASQ as root user to avoid SHM permission issues
2019-02-04 01:30:42 +01:00
if grep -r -q '^\s*user=' /etc/dnsmasq.* ; then
# Change user that had been set previously to root
2019-02-05 07:17:54 +01:00
for f in $( grep -r -l '^\s*user=' /etc/dnsmasq.*) ; do
2019-02-04 01:37:03 +01:00
sed -i " /^\s*user=/ c\user= ${ DNSMASQ_USER } " " ${ f } "
2019-02-04 01:30:42 +01:00
done
else
2019-02-04 01:37:03 +01:00
echo -e " \nuser= ${ DNSMASQ_USER } " >> /etc/dnsmasq.conf
2019-02-04 01:30:42 +01:00
fi
2019-02-03 19:47:37 +01:00
}
2022-07-12 00:50:05 +02:00
setup_FTL_Interface( ) {
local interface = " ${ INTERFACE :- eth0 } "
2016-10-30 18:13:52 +01:00
2022-07-12 00:50:05 +02:00
# Set the interface for FTL to listen on
local interfaceType = 'default'
if [ " $interface " != 'eth0' ] ; then
interfaceType = 'custom'
2016-10-30 18:13:52 +01:00
fi ;
2022-07-12 00:50:05 +02:00
echo " FTL binding to $interfaceType interface: $interface "
change_setting "PIHOLE_INTERFACE" " ${ interface } "
2016-10-30 18:13:52 +01:00
}
2022-07-12 00:50:05 +02:00
setup_FTL_CacheSize( ) {
2020-10-11 02:01:18 +02:00
local warning = "WARNING: CUSTOM_CACHE_SIZE not used"
local dnsmasq_pihole_01_location = "/etc/dnsmasq.d/01-pihole.conf"
# Quietly exit early for empty or default
2022-07-12 00:50:05 +02:00
if [ [ -z " ${ CUSTOM_CACHE_SIZE } " || " ${ CUSTOM_CACHE_SIZE } " = = '10000' ] ] ; then return ; fi
2020-10-11 02:01:18 +02:00
if [ [ " ${ DNSSEC } " = = "true" ] ] ; then
echo " $warning - Cannot change cache size if DNSSEC is enabled "
return
fi
2022-07-12 00:50:05 +02:00
if ! echo " $CUSTOM_CACHE_SIZE " | grep -q '^[0-9]*$' ; then
echo " $warning - $CUSTOM_CACHE_SIZE is not an integer "
2020-10-11 02:01:18 +02:00
return
fi
2022-07-12 00:50:05 +02:00
local -i custom_cache_size = " $CUSTOM_CACHE_SIZE "
if ( ( custom_cache_size < 0 ) ) ; then
2020-10-11 02:01:18 +02:00
echo " $warning - $custom_cache_size is not a positive integer or zero "
return
fi
echo " Custom CUSTOM_CACHE_SIZE set to $custom_cache_size "
2022-07-12 00:50:05 +02:00
change_setting "CACHE_SIZE" " $custom_cache_size "
2020-10-11 02:01:18 +02:00
sed -i " s/^cache-size=\s*[0-9]*/cache-size= $custom_cache_size / " ${ dnsmasq_pihole_01_location }
}
2022-07-12 00:50:05 +02:00
apply_FTL_Configs_From_Env( ) {
# Get all exported environment variables starting with FTLCONF_ as a prefix and call the changeFTLsetting
# function with the environment variable's suffix as the key. This allows applying any pihole-FTL.conf
# setting defined here: https://docs.pi-hole.net/ftldns/configfile/
declare -px | grep FTLCONF_ | sed -E 's/declare -x FTLCONF_([^=]+)=\"(.+)\"/\1 \2/' | while read -r name value
do
echo " Applying pihole-FTL.conf setting $name = $value "
changeFTLsetting " $name " " $value "
done
}
setup_FTL_dhcp( ) {
if [ -z " ${ DHCP_START } " ] || [ -z " ${ DHCP_END } " ] || [ -z " ${ DHCP_ROUTER } " ] ; then
echo "ERROR: Won't enable DHCP server because mandatory Environment variables are missing: DHCP_START, DHCP_END and/or DHCP_ROUTER"
change_setting "DHCP_ACTIVE" "false"
else
change_setting "DHCP_ACTIVE" " ${ DHCP_ACTIVE } "
change_setting "DHCP_START" " ${ DHCP_START } "
change_setting "DHCP_END" " ${ DHCP_END } "
change_setting "DHCP_ROUTER" " ${ DHCP_ROUTER } "
change_setting "DHCP_LEASETIME" " ${ DHCP_LEASETIME } "
change_setting "PIHOLE_DOMAIN" " ${ PIHOLE_DOMAIN } "
change_setting "DHCP_IPv6" " ${ DHCP_IPv6 } "
change_setting "DHCP_rapid_commit" " ${ DHCP_rapid_commit } "
fi
}
setup_FTL_query_logging( ) {
if [ " ${ QUERY_LOGGING_OVERRIDE } " = = "false" ] ; then
echo "::: Disabling Query Logging"
change_setting "QUERY_LOGGING" " $QUERY_LOGGING_OVERRIDE "
removeKey " ${ dnsmasqconfig } " log-queries
else
# If it is anything other than false, set it to true
change_setting "QUERY_LOGGING" "true"
# Set pihole logging on for good measure
echo "::: Enabling Query Logging"
addKey " ${ dnsmasqconfig } " log-queries
fi
}
setup_FTL_server( ) {
[ -n " ${ REV_SERVER } " ] && change_setting "REV_SERVER" " $REV_SERVER "
[ -n " ${ REV_SERVER_DOMAIN } " ] && change_setting "REV_SERVER_DOMAIN" " $REV_SERVER_DOMAIN "
[ -n " ${ REV_SERVER_TARGET } " ] && change_setting "REV_SERVER_TARGET" " $REV_SERVER_TARGET "
[ -n " ${ REV_SERVER_CIDR } " ] && change_setting "REV_SERVER_CIDR" " $REV_SERVER_CIDR "
if [ -z " $REV_SERVER " ] ; then
# If the REV_SERVER* variables are set, then there is no need to add these.
# If it is not set, then adding these variables is fine, and they will be converted by the Pi-hole install script
[ -n " ${ CONDITIONAL_FORWARDING } " ] && change_setting "CONDITIONAL_FORWARDING" " $CONDITIONAL_FORWARDING "
[ -n " ${ CONDITIONAL_FORWARDING_IP } " ] && change_setting "CONDITIONAL_FORWARDING_IP" " $CONDITIONAL_FORWARDING_IP "
[ -n " ${ CONDITIONAL_FORWARDING_DOMAIN } " ] && change_setting "CONDITIONAL_FORWARDING_DOMAIN" " $CONDITIONAL_FORWARDING_DOMAIN "
[ -n " ${ CONDITIONAL_FORWARDING_REVERSE } " ] && change_setting "CONDITIONAL_FORWARDING_REVERSE" " $CONDITIONAL_FORWARDING_REVERSE "
fi
}
setup_FTL_upstream_DNS( ) {
if [ -z " ${ PIHOLE_DNS_ } " ] ; then
# For backward compatibility, if DNS1 and/or DNS2 are set, but PIHOLE_DNS_ is not, convert them to
# a semi-colon delimited string and store in PIHOLE_DNS_
# They are not used anywhere if PIHOLE_DNS_ is set already
[ -n " ${ DNS1 } " ] && echo "Converting DNS1 to PIHOLE_DNS_" && PIHOLE_DNS_ = " $DNS1 "
[ [ -n " ${ DNS2 } " && " ${ DNS2 } " != "no" ] ] && echo "Converting DNS2 to PIHOLE_DNS_" && PIHOLE_DNS_ = " $PIHOLE_DNS_ ; $DNS2 "
fi
# Parse the PIHOLE_DNS variable, if it exists, and apply upstream servers to Pi-hole config
if [ -n " ${ PIHOLE_DNS_ } " ] ; then
echo "Setting DNS servers based on PIHOLE_DNS_ variable"
# Remove any PIHOLE_DNS_ entries from setupVars.conf, if they exist
sed -i '/PIHOLE_DNS_/d' /etc/pihole/setupVars.conf
# Split into an array (delimited by ;)
# Loop through and add them one by one to setupVars.conf
IFS = ";" read -r -a PIHOLE_DNS_ARR <<< " ${ PIHOLE_DNS_ } "
count = 1
valid_entries = 0
for i in " ${ PIHOLE_DNS_ARR [@] } " ; do
2022-08-26 23:40:15 +02:00
# Ensure we don't have an empty value first (see https://github.com/pi-hole/docker-pi-hole/issues/1174#issuecomment-1228763422 )
if [ -n " $i " ] ; then
if valid_ip " $i " || valid_ip6 " $i " ; then
change_setting " PIHOLE_DNS_ $count " " $i "
2022-07-12 00:50:05 +02:00
( ( count = count+1) )
( ( valid_entries = valid_entries+1) )
continue
2022-08-26 23:40:15 +02:00
fi
# shellcheck disable=SC2086
if [ -n " $( dig +short ${ i //#*/ } ) " ] ; then
# If the "address" is a domain (for example a docker link) then try to resolve it and add
# the result as a DNS server in setupVars.conf.
resolved_ip = " $( dig +short ${ i //#*/ } | head -n 1) "
if [ -n " ${ i //*#/ } " ] && [ " ${ i //*#/ } " != " ${ i //#*/ } " ] ; then
resolved_ip = " ${ resolved_ip } # ${ i //*#/ } "
fi
echo " Resolved ${ i } from PIHOLE_DNS_ as: ${ resolved_ip } "
if valid_ip " $resolved_ip " || valid_ip6 " $resolved_ip " ; then
change_setting " PIHOLE_DNS_ $count " " $resolved_ip "
( ( count = count+1) )
( ( valid_entries = valid_entries+1) )
continue
fi
fi
# If the above tests fail then this is an invalid DNS server
echo " Invalid entry detected in PIHOLE_DNS_: ${ i } "
2022-07-12 00:50:05 +02:00
fi
done
if [ $valid_entries -eq 0 ] ; then
2022-08-26 23:40:15 +02:00
echo "No Valid entries detected in PIHOLE_DNS_. Aborting"
exit 1
2022-07-12 00:50:05 +02:00
fi
else
# Environment variable has not been set, but there may be existing values in an existing setupVars.conf
# if this is the case, we do not want to overwrite these with the defaults of 8.8.8.8 and 8.8.4.4
# Pi-hole can run with only one upstream configured, so we will just check for one.
setupVarsDNS = " $( grep 'PIHOLE_DNS_' /etc/pihole/setupVars.conf || true ) "
if [ -z " ${ setupVarsDNS } " ] ; then
echo "Configuring default DNS servers: 8.8.8.8, 8.8.4.4"
change_setting "PIHOLE_DNS_1" "8.8.8.8"
change_setting "PIHOLE_DNS_2" "8.8.4.4"
else
echo "Existing DNS servers detected in setupVars.conf. Leaving them alone"
fi
fi
}
2022-07-12 16:48:12 +02:00
setup_FTL_ProcessDNSSettings( ) {
# Commit settings to 01-pihole.conf
# shellcheck source=/dev/null
. /opt/pihole/webpage.sh
ProcessDNSSettings
}
2017-07-26 21:36:32 +02:00
setup_lighttpd_bind( ) {
2022-08-22 22:42:29 +02:00
local serverip = " ${ FTLCONF_LOCAL_IPV4 } "
# if using '--net=host' only bind lighttpd on $FTLCONF_LOCAL_IPV4 and localhost
2020-05-30 06:56:32 +02:00
if grep -q "docker" /proc/net/dev && [ [ $serverip != 0.0.0.0 ] ] ; then #docker (docker0 by default) should only be present on the host system
2018-07-30 01:14:24 +02:00
if ! grep -q "server.bind" /etc/lighttpd/lighttpd.conf ; then # if the declaration is already there, don't add it again
sed -i -E " s/server\.port\s+\=\s+([0-9]+)/server.bind\t\t = \" ${ serverip } \"\nserver.port\t\t = \1\n " \$ SERVER"\[\"socket\"\] == \"127\.0\.0\.1:\1\" \{\}/" /etc/lighttpd/lighttpd.conf
2017-07-26 23:50:23 +02:00
fi
fi
2017-07-26 21:36:32 +02:00
}
2022-07-12 00:50:05 +02:00
setup_web_php_env( ) {
2016-10-07 02:46:20 +02:00
if [ -z " $VIRTUAL_HOST " ] ; then
2022-08-22 22:42:29 +02:00
VIRTUAL_HOST = " $FTLCONF_LOCAL_IPV4 "
2016-10-07 02:46:20 +02:00
fi ;
2021-12-28 23:20:32 +01:00
2022-07-12 00:50:05 +02:00
for config_var in "VIRTUAL_HOST" "CORS_HOSTS" "PHP_ERROR_LOG" "PIHOLE_DOCKER_TAG" "TZ" ; do
2021-12-28 23:20:32 +01:00
local beginning_of_line = " \t\t\t\" ${ config_var } \" => "
if grep -qP " $beginning_of_line " " $PHP_ENV_CONFIG " ; then
# replace line if already present
sed -i " / ${ beginning_of_line } /c\\ ${ beginning_of_line } \" ${ !config_var } \", " " $PHP_ENV_CONFIG "
else
# add line otherwise
sed -i " /bin-environment/ a\\ ${ beginning_of_line } \" ${ !config_var } \", " " $PHP_ENV_CONFIG "
fi
done
2016-10-07 02:46:20 +02:00
echo "Added ENV to php:"
2022-07-12 00:50:05 +02:00
grep -E '(VIRTUAL_HOST|CORS_HOSTS|PHP_ERROR_LOG|PIHOLE_DOCKER_TAG|TZ)' " $PHP_ENV_CONFIG "
2016-10-07 02:46:20 +02:00
}
2017-11-15 05:33:05 +01:00
setup_web_port( ) {
local warning = "WARNING: Custom WEB_PORT not used"
# Quietly exit early for empty or default
2022-07-12 00:50:05 +02:00
if [ [ -z " ${ WEB_PORT } " || " ${ WEB_PORT } " = = '80' ] ] ; then return ; fi
2017-11-15 05:33:05 +01:00
2022-07-12 00:50:05 +02:00
if ! echo " $WEB_PORT " | grep -q '^[0-9][0-9]*$' ; then
echo " $warning - $WEB_PORT is not an integer "
2017-11-15 05:33:05 +01:00
return
fi
2022-07-12 00:50:05 +02:00
local -i web_port = " $WEB_PORT "
if ( ( web_port < 1 || web_port > 65535 ) ) ; then
2017-11-15 05:33:05 +01:00
echo " $warning - $web_port is not within valid port range of 1-65535 "
return
fi
echo " Custom WEB_PORT set to $web_port "
2022-08-22 22:42:29 +02:00
echo " INFO: Without proper router DNAT forwarding to $FTLCONF_LOCAL_IPV4 : $web_port , you may not get any blocked websites on ads "
2018-02-23 06:11:53 +01:00
2020-01-14 15:37:44 +01:00
# Update lighttpd's port
2022-07-12 00:50:05 +02:00
sed -i '/server.port\s*=\s*80\s*$/ s/80/' " ${ WEB_PORT } " '/g' /etc/lighttpd/lighttpd.conf
}
2017-11-15 05:33:05 +01:00
2022-07-12 00:50:05 +02:00
setup_web_theme( ) {
# Parse the WEBTHEME variable, if it exists, and set the selected theme if it is one of the supported values.
# If an invalid theme name was supplied, setup WEBTHEME to use the default-light theme.
if [ -n " ${ WEBTHEME } " ] ; then
case " ${ WEBTHEME } " in
"default-dark" | "default-darker" | "default-light" | "default-auto" | "lcars" )
echo " Setting Web Theme based on WEBTHEME variable, using value ${ WEBTHEME } "
change_setting "WEBTHEME" " ${ WEBTHEME } "
; ;
*)
echo " Invalid theme name supplied: ${ WEBTHEME } , falling back to default-light. "
change_setting "WEBTHEME" "default-light"
; ;
esac
fi
2017-11-15 05:33:05 +01:00
}
2020-02-26 18:27:42 +01:00
load_web_password_secret( ) {
# If WEBPASSWORD is not set at all, attempt to read password from WEBPASSWORD_FILE,
# allowing secrets to be passed via docker secrets
if [ -z " ${ WEBPASSWORD +x } " ] && [ -n " ${ WEBPASSWORD_FILE } " ] && [ -r " ${ WEBPASSWORD_FILE } " ] ; then
WEBPASSWORD = $( <" ${ WEBPASSWORD_FILE } " )
fi ;
}
2018-09-18 07:11:26 +02:00
setup_web_password( ) {
2022-05-30 08:00:16 +02:00
if [ -z " ${ WEBPASSWORD +x } " ] ; then
2022-07-19 19:22:49 +02:00
# ENV WEBPASSWORD_OVERRIDE is not set
2022-05-30 08:00:16 +02:00
# Exit if setupvars already has a password
setup_var_exists "WEBPASSWORD" && return
2022-06-06 21:12:24 +02:00
# Generate new random password
WEBPASSWORD = $( tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8)
echo " Assigning random password: $WEBPASSWORD "
2022-05-30 08:00:16 +02:00
else
2022-07-19 19:22:49 +02:00
# ENV WEBPASSWORD_OVERRIDE is set and will be used
2022-05-30 22:31:57 +02:00
echo "::: Assigning password defined by Environment Variable"
2022-07-19 19:22:49 +02:00
# WEBPASSWORD="$WEBPASSWORD"
2022-05-30 08:00:16 +02:00
fi
2021-04-14 18:13:01 +02:00
# Explicitly turn off bash printing when working with secrets
{ set +x; } 2>/dev/null
2022-07-19 19:22:49 +02:00
if [ [ " $WEBPASSWORD " = = "" ] ] ; then
2018-08-04 22:58:05 +02:00
echo "" | pihole -a -p
else
2022-07-19 19:22:49 +02:00
pihole -a -p " $WEBPASSWORD " " $WEBPASSWORD "
2018-08-04 22:58:05 +02:00
fi
2018-10-17 02:55:52 +02:00
# To avoid printing this if conditional in bash debug, turn off debug above..
# then re-enable debug if necessary (more code but cleaner printed output)
if [ " ${ PH_VERBOSE :- 0 } " -gt 0 ] ; then
set -x
2018-08-04 22:58:05 +02:00
fi
2017-02-09 05:46:46 +01:00
}
2017-10-14 14:53:29 +02:00
2016-10-21 05:36:55 +02:00
setup_ipv4_ipv6( ) {
local ip_versions = "IPv4 and IPv6"
2021-04-09 20:14:39 +02:00
if [ " ${ IPv6 ,, } " != "true" ] ; then
2016-10-21 05:36:55 +02:00
ip_versions = "IPv4"
2018-07-30 01:14:24 +02:00
sed -i '/use-ipv6.pl/ d' /etc/lighttpd/lighttpd.conf
2016-10-21 05:36:55 +02:00
fi ;
echo " Using $ip_versions "
}
2016-10-07 02:46:20 +02:00
test_configs( ) {
2016-10-07 19:14:12 +02:00
set -e
echo -n '::: Testing lighttpd config: '
2018-01-25 19:45:05 +01:00
lighttpd -t -f /etc/lighttpd/lighttpd.conf || exit 1
2016-10-07 19:14:12 +02:00
set +e
2018-07-30 01:14:24 +02:00
echo "::: All config checks passed, cleared for startup ..."
2016-10-07 02:46:20 +02:00
}
2018-07-30 01:14:24 +02:00
setup_blocklists( ) {
# Exit/return early without setting up adlists with defaults for any of the following conditions:
2019-02-17 07:18:28 +01:00
# 1. skip_setup_blocklists env is set
2018-07-30 01:14:24 +02:00
exit_string = " (exiting ${ FUNCNAME [0] } early) "
2019-02-17 07:18:28 +01:00
if [ -n " ${ skip_setup_blocklists } " ] ; then
echo " ::: skip_setup_blocklists requested ( $exit_string ) "
2018-07-30 01:14:24 +02:00
return
fi
# 2. The adlist file exists already (restarted container or volume mounted list)
if [ -f " ${ adlistFile } " ] ; then
echo " ::: Preexisting ad list ${ adlistFile } detected ( $exit_string ) "
cat " ${ adlistFile } "
return
fi
echo " ::: ${ FUNCNAME [0] } now setting default blocklists up: "
echo " ::: TIP: Use a docker volume for ${ adlistFile } if you want to customize for first boot "
2019-01-22 05:43:38 +01:00
installDefaultBlocklists
2018-07-30 01:14:24 +02:00
echo " ::: Blocklists ( ${ adlistFile } ) now set to: "
cat " ${ adlistFile } "
2016-10-07 02:46:20 +02:00
}
2019-02-17 07:18:28 +01:00
setup_var_exists( ) {
local KEY = " $1 "
if [ -n " $2 " ] ; then
local REQUIRED_VALUE = "[^\n]+"
fi
if grep -Pq " ^ ${ KEY } = ${ REQUIRED_VALUE } " " $setupVars " ; then
echo " ::: Pre existing ${ KEY } found "
true
else
false
fi
}
2022-07-12 00:50:05 +02:00
setup_web_temp_unit( ) {
local UNIT = " ${ TEMPERATUREUNIT } "
2020-05-04 23:15:35 +02:00
# check if var is empty
if [ [ " $UNIT " != "" ] ] ; then
# check if we have valid units
if [ [ " $UNIT " = = "c" || " $UNIT " = = "k" || $UNIT = = "f" ] ] ; then
2022-07-12 00:50:05 +02:00
pihole -a -" ${ UNIT } "
2020-05-04 23:15:35 +02:00
fi
fi
}
2022-07-12 00:50:05 +02:00
setup_web_layout( ) {
local LO = " ${ WEBUIBOXEDLAYOUT } "
2020-05-04 23:15:35 +02:00
# check if var is empty
if [ [ " $LO " != "" ] ] ; then
# check if we have valid types boxed | traditional
if [ [ " $LO " = = "traditional" || " $LO " = = "boxed" ] ] ; then
change_setting "WEBUIBOXEDLAYOUT" " $WEBUIBOXEDLAYOUT "
fi
fi
}
setup_admin_email( ) {
2022-07-12 00:50:05 +02:00
local EMAIL = " ${ ADMIN_EMAIL } "
2020-05-04 23:15:35 +02:00
# check if var is empty
if [ [ " $EMAIL " != "" ] ] ; then
pihole -a -e " $EMAIL "
fi
}