2017-03-04 06:13:11 +01:00
#!/bin/bash
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
2023-02-16 00:48:53 +01:00
# . /opt/pihole/utils.sh
#######################
# returns value from FTLs config file using pihole-FTL --config
#
# Takes one argument: key
# Example getFTLConfigValue dns.piholePTR
#######################
2023-10-20 14:14:08 +02:00
getFTLConfigValue( ) {
pihole-FTL --config -q " ${ 1 } "
2023-02-16 00:48:53 +01:00
}
#######################
# sets value in FTLs config file using pihole-FTL --config
#
# Takes two arguments: key and value
# Example setFTLConfigValue dns.piholePTR PI.HOLE
#
# Note, for complex values such as dns.upstreams, you should wrap the value in single quotes:
# setFTLConfigValue dns.upstreams '[ "8.8.8.8" , "8.8.4.4" ]'
#######################
2023-10-20 14:14:08 +02:00
setFTLConfigValue( ) {
pihole-FTL --config " ${ 1 } " " ${ 2 } " >/dev/null
2023-02-16 00:48:53 +01:00
}
# shellcheck disable=SC2034
ensure_basic_configuration( ) {
2024-03-10 20:29:38 +01:00
# Force a check of pihole-FTL --config, this will read any environment variables and set them in the config file
# suppress the output as we don't need to see the default values.
getFTLConfigValue >/dev/null
echo ""
2023-02-16 00:48:53 +01:00
echo " [i] Ensuring basic configuration by re-running select functions from basic-install.sh"
mkdir -p /var/run/pihole /var/log/pihole
touch /var/log/pihole/FTL.log /var/log/pihole/pihole.log
chown -R pihole:pihole /var/run/pihole /var/log/pihole
2023-08-10 00:31:39 +02:00
if [ [ -z " ${ PYTEST } " ] ] ; then
if [ [ ! -f /etc/pihole/adlists.list ] ] ; then
echo "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" >/etc/pihole/adlists.list
fi
2023-08-09 23:13:28 +02:00
fi
2023-08-10 00:31:39 +02:00
2023-02-16 00:48:53 +01:00
chown -R pihole:pihole /etc/pihole
2024-03-10 20:29:38 +01:00
# Install the logrotate config file - this is done already in Dockerfile
# but if a user has mounted a volume over /etc/pihole, it will have been lost
# pihole-FTL-prestart.sh will set the ownership of the file to root:root
install -Dm644 -t /etc/pihole /etc/.pihole/advanced/Templates/logrotate
2023-06-29 18:25:08 +02:00
# If FTLCONF_files_macvendor is not set
if [ [ -z " ${ FTLCONF_files_macvendor :- } " ] ] ; then
# User is not passing in a custom location - so force FTL to use the file we moved to / during the build
setFTLConfigValue "files.macvendor" "/macvendor.db"
chown pihole:pihole /macvendor.db
fi
2023-11-06 22:43:42 +01:00
2024-03-10 20:29:38 +01:00
# If getFTLConfigValue "dns.upstreams" returns [], exit the container. We need upstream servers to function!
if [ [ $( getFTLConfigValue "dns.upstreams" ) = = "[]" ] ] ; then
echo ""
echo " [X] No DNS upstream servers are set!"
echo " [i] Recommended: Set the upstream DNS servers in the environment variable FTLCONF_dns_upstream"
echo ""
exit 1
fi
2022-07-12 00:50:05 +02:00
2024-03-11 19:14:26 +01:00
setup_web_password
}
setup_web_password( ) {
2024-03-10 20:29:38 +01:00
# If FTLCONF_webserver_api_password is not set
2023-10-20 14:14:08 +02:00
if [ -z " ${ FTLCONF_webserver_api_password +x } " ] ; then
2024-03-10 20:29:38 +01:00
# Is this already set to something other than blank (default) in FTL's config file? (maybe in a volume mount)
2023-10-28 20:47:59 +02:00
if [ [ $( pihole-FTL --config webserver.api.pwhash) = \$ BALLOON-SHA256* ] ] ; then
2023-10-20 14:14:08 +02:00
echo " [i] Password already set in config file"
return
2024-03-10 20:29:38 +01:00
else
# If we are here, the password is set in neither the environment nor the config file
# We will generate a random password.
RANDOMPASSWORD = $( tr -dc _A-Z-a-z-0-9 </dev/urandom | head -c 8)
echo " [i] No password set in environment or config file, assigning random password: $RANDOMPASSWORD "
# Explicitly turn off bash printing when working with secrets
{ set +x; } 2>/dev/null
pihole-FTL --config webserver.api.password " $RANDOMPASSWORD " >/dev/null
# 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
fi
2023-10-20 14:14:08 +02:00
fi
else
echo " [i] Assigning password defined by Environment Variable"
fi
}
2022-07-12 00:50:05 +02: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:
2022-08-29 14:22:50 +02:00
echo " [i] Setting capabilities on pihole-FTL where possible"
2023-02-16 00:48:53 +01:00
capsh --has-p= cap_chown 2>/dev/null && CAP_STR += ',CAP_CHOWN'
capsh --has-p= cap_net_bind_service 2>/dev/null && CAP_STR += ',CAP_NET_BIND_SERVICE'
capsh --has-p= cap_net_raw 2>/dev/null && CAP_STR += ',CAP_NET_RAW'
capsh --has-p= cap_net_admin 2>/dev/null && CAP_STR += ',CAP_NET_ADMIN' || DHCP_READY = 'false'
capsh --has-p= cap_sys_nice 2>/dev/null && CAP_STR += ',CAP_SYS_NICE'
2022-08-24 22:27:38 +02:00
if [ [ ${ CAP_STR } ] ] ; then
# We have the (some of) the above caps available to us - apply them to pihole-FTL
2022-08-29 14:17:22 +02:00
echo " [i] Applying the following caps to pihole-FTL:"
2023-10-20 14:14:08 +02:00
IFS = ',' read -ra CAPS <<< " ${ CAP_STR : 1 } "
2022-08-29 17:49:16 +02:00
for i in " ${ CAPS [@] } " ; do
echo " * ${ i } "
done
2022-08-24 22:27:38 +02:00
setcap ${ CAP_STR : 1 } +ep " $( which pihole-FTL) " || ret = $?
2023-02-06 00:55:24 +01:00
if [ [ $DHCP_READY = = false ] ] && [ [ $FTLCONF_dhcp_active = = true ] ] ; then
2022-08-24 22:27:38 +02:00
# 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'
2023-02-05 22:21:49 +01:00
setFTLConfigValue dhcp.active false
2022-08-24 22:27:38 +02:00
fi
if [ [ $ret -ne 0 && " ${ DNSMASQ_USER :- pihole } " != "root" ] ] ; then
2022-08-29 14:17:22 +02:00
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'"
2022-08-24 22:27:38 +02:00
exit 1
fi
else
2022-08-29 14:17:22 +02:00
echo " [!] WARNING: Unable to set capabilities for pihole-FTL."
echo " Please ensure that the container has the required capabilities."
2022-08-24 22:27:38 +02:00
exit 1
fi
2023-10-20 14:14:08 +02:00
echo ""
2022-08-24 22:27:38 +02:00
}