2017-03-04 06:13:11 +01:00
#!/bin/bash
2017-01-18 05:49:42 +01:00
. /opt/pihole/webpage.sh
2017-03-04 06:13:11 +01:00
setupVars = " $setupVars "
ServerIP = " $ServerIP "
2017-05-14 00:03:24 +02:00
ServerIPv6 = " $ServerIPv6 "
2017-03-04 06:13:11 +01:00
IPv6 = " $IPv6 "
2017-01-08 17:42:39 +01:00
2017-01-21 22:02:08 +01:00
prepare_setup_vars( ) {
2017-01-22 03:12:37 +01:00
touch " $setupVars "
2017-01-21 22:02:08 +01:00
}
2016-10-07 02:46:20 +02:00
validate_env( ) {
if [ -z " $ServerIP " ] ; then
echo "ERROR: To function correctly you must pass an environment variables of 'ServerIP' into the docker container with the IP of your docker host from which you are passing web (80) and dns (53) ports from"
exit 1
fi ;
2017-05-19 07:36:53 +02:00
# Debian
nc_error = 'Name or service not known'
2017-10-04 18:05:27 +02:00
if [ [ " $TAG " = = 'alpine' ] ] ; then
2017-05-19 07:36:53 +02:00
nc_error = 'bad address'
fi ;
# Required ServerIP is a valid IP
if nc -w1 -z " $ServerIP " 53 2>& 1 | grep -q " $nc_error " ; then
echo " ERROR: ServerIP Environment variable ( $ServerIP ) doesn't appear to be a valid IPv4 address "
exit 1
fi
# Optional IPv6 is a valid address
if [ [ -n " $ServerIPv6 " ] ] ; then
if [ [ " $ServerIPv6 " = = 'kernel' ] ] ; then
echo "WARNING: You passed in IPv6 with a value of 'kernel', this maybe beacuse you do not have IPv6 enabled on your network"
unset ServerIPv6
return
fi
if nc -w 1 -z " $ServerIPv6 " 53 2>& 1 | grep -q " $nc_error " || ! ip route get " $ServerIPv6 " ; then
echo " ERROR: ServerIPv6 Environment variable ( $ServerIPv6 ) doesn't appear to be a valid IPv6 address "
echo " TIP: If your server is not IPv6 enabled just remove '-e ServerIPv6' from your docker container"
exit 1
fi
fi ;
2016-10-07 02:46:20 +02:00
}
2016-10-30 18:13:52 +01:00
setup_dnsmasq_dns( ) {
2017-01-18 05:49:42 +01:00
. /opt/pihole/webpage.sh
2016-10-07 02:46:20 +02:00
local DNS1 = " ${ 1 :- 8 .8.8.8 } "
local DNS2 = " ${ 2 :- 8 .8.4.4 } "
2016-10-08 20:34:23 +02:00
local dnsType = 'default'
2016-10-07 02:46:20 +02:00
if [ " $DNS1 " != '8.8.8.8' ] || [ " $DNS2 " != '8.8.4.4' ] ; then
dnsType = 'custom'
fi ;
echo " Using $dnsType DNS servers: $DNS1 & $DNS2 "
2017-01-18 05:49:42 +01:00
[ -n " $DNS1 " ] && change_setting "PIHOLE_DNS_1" " ${ DNS1 } "
[ -n " $DNS2 " ] && change_setting "PIHOLE_DNS_2" " ${ DNS2 } "
2017-03-05 00:31:09 +01:00
}
setup_dnsmasq_interface( ) {
local INTERFACE = " ${ 1 :- eth0 } "
local interfaceType = 'default'
if [ " $INTERFACE " != 'eth0' ] ; then
interfaceType = 'custom'
fi ;
echo " DNSMasq binding to $interfaceType interface: $INTERFACE "
[ -n " $INTERFACE " ] && change_setting "PIHOLE_INTERFACE" " ${ INTERFACE } "
}
2017-05-23 16:22:16 +02:00
setup_dnsmasq_config_if_missing( ) {
# When fresh empty directory volumes are used we miss this file
if [ ! -f /etc/dnsmasq.d/01-pihole.conf ] ; then
cp /etc/.pihole/advanced/01-pihole.conf /etc/dnsmasq.d/
fi ;
}
2017-03-05 00:31:09 +01:00
setup_dnsmasq( ) {
# Coordinates
2017-05-23 16:22:16 +02:00
setup_dnsmasq_config_if_missing
2017-03-05 00:31:09 +01:00
setup_dnsmasq_dns " $DNS1 " " $DNS2 "
setup_dnsmasq_interface " $INTERFACE "
ProcessDNSSettings
2016-10-07 02:46:20 +02:00
}
2016-10-30 18:13:52 +01:00
setup_dnsmasq_hostnames( ) {
# largely borrowed from automated install/basic-install.sh
2017-01-08 17:42:39 +01:00
local IPV4_ADDRESS = " ${ 1 } "
local IPV6_ADDRESS = " ${ 2 } "
2016-10-30 18:13:52 +01:00
local hostname = " ${ 3 } "
2016-10-30 18:25:08 +01:00
local dnsmasq_pihole_01_location = "/etc/dnsmasq.d/01-pihole.conf"
2016-10-30 18:13:52 +01:00
if [ -z " $hostname " ] ; then
2016-10-30 18:25:08 +01:00
if [ [ -f /etc/hostname ] ] ; then
hostname = $( </etc/hostname)
elif [ -x " $( command -v hostname) " ] ; then
hostname = $( hostname -f)
fi
2016-10-30 18:13:52 +01:00
fi ;
2017-01-08 17:42:39 +01:00
if [ [ " ${ IPV4_ADDRESS } " != "" ] ] ; then
tmp = ${ IPV4_ADDRESS %/* }
sed -i " s/@IPV4@/ $tmp / " ${ dnsmasq_pihole_01_location }
2016-10-30 18:25:08 +01:00
else
2017-01-08 17:42:39 +01:00
sed -i '/^address=\/pi.hole\/@IPV4@/d' ${ dnsmasq_pihole_01_location }
sed -i '/^address=\/@HOSTNAME@\/@IPV4@/d' ${ dnsmasq_pihole_01_location }
2016-10-30 18:25:08 +01:00
fi
2017-01-08 17:42:39 +01:00
if [ [ " ${ IPV6_ADDRESS } " != "" ] ] ; then
sed -i " s/@IPv6@/ $IPV6_ADDRESS / " ${ dnsmasq_pihole_01_location }
2016-10-30 18:25:08 +01:00
else
sed -i '/^address=\/pi.hole\/@IPv6@/d' ${ dnsmasq_pihole_01_location }
sed -i '/^address=\/@HOSTNAME@\/@IPv6@/d' ${ dnsmasq_pihole_01_location }
fi
if [ [ " ${ hostname } " != "" ] ] ; then
sed -i " s/@HOSTNAME@/ $hostname / " ${ dnsmasq_pihole_01_location }
else
sed -i '/^address=\/@HOSTNAME@*/d' ${ dnsmasq_pihole_01_location }
fi
2016-10-30 18:13:52 +01:00
}
2017-07-26 21:36:32 +02:00
setup_lighttpd_bind( ) {
2017-10-04 18:05:27 +02:00
if [ [ " $TAG " = = 'debian' ] ] ; then
2017-07-26 23:50:23 +02:00
# if using '--net=host' only bind lighttpd on $ServerIP
if grep -q "docker" /proc/net/dev ; then #docker (docker0 by default) should only be present on the host system
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+80/server.bind\t\t = \" ${ ServerIP } \"\nserver.port\t\t = 80/ " /etc/lighttpd/lighttpd.conf
fi
fi
fi
2017-07-26 21:36:32 +02:00
}
2016-10-07 02:46:20 +02:00
setup_php_env( ) {
2017-10-04 18:05:27 +02:00
case $TAG in
2016-10-21 05:36:55 +02:00
"debian" ) setup_php_env_debian ; ;
"alpine" ) setup_php_env_alpine ; ;
esac
2016-10-07 02:46:20 +02:00
}
setup_php_env_debian( ) {
if [ -z " $VIRTUAL_HOST " ] ; then
VIRTUAL_HOST = " $ServerIP "
fi ;
2016-10-21 05:36:55 +02:00
local vhost_line = " \t\t\t\"VIRTUAL_HOST\" => \" ${ VIRTUAL_HOST } \", "
local serverip_line = " \t\t\t\"ServerIP\" => \" ${ ServerIP } \", "
local php_error_line = " \t\t\t\"PHP_ERROR_LOG\" => \" ${ PHP_ERROR_LOG } \", "
# idempotent line additions
2016-11-02 04:46:29 +01:00
grep -qP " $vhost_line " " $PHP_ENV_CONFIG " || \
2016-10-22 20:39:12 +02:00
sed -i " /bin-environment/ a\\ ${ vhost_line } " " $PHP_ENV_CONFIG "
2016-11-02 04:46:29 +01:00
grep -qP " $serverip_line " " $PHP_ENV_CONFIG " || \
2016-10-22 20:39:12 +02:00
sed -i " /bin-environment/ a\\ ${ serverip_line } " " $PHP_ENV_CONFIG "
2016-11-02 04:46:29 +01:00
grep -qP " $php_error_line " " $PHP_ENV_CONFIG " || \
2016-10-22 20:39:12 +02:00
sed -i " /bin-environment/ a\\ ${ php_error_line } " " $PHP_ENV_CONFIG "
2016-10-07 02:46:20 +02:00
echo "Added ENV to php:"
2016-10-22 20:39:12 +02:00
grep -E '(VIRTUAL_HOST|ServerIP|PHP_ERROR_LOG)' " $PHP_ENV_CONFIG "
2016-10-07 02:46:20 +02:00
}
setup_php_env_alpine( ) {
2016-10-30 18:25:08 +01:00
# Intentionally tabs, required by HEREDOC de-indentation (<<-)
2016-10-08 21:56:06 +02:00
cat <<-EOF > "$PHP_E NV_CONFIG"
[ www]
env[ PATH] = ${ PATH }
env[ PHP_ERROR_LOG] = ${ PHP_ERROR_LOG }
env[ ServerIP] = ${ ServerIP }
EOF
2016-10-07 02:46:20 +02:00
if [ -z " $VIRTUAL_HOST " ] ; then
VIRTUAL_HOST = " $ServerIP "
fi ;
2016-10-08 21:56:06 +02:00
echo " env[VIRTUAL_HOST] = ${ VIRTUAL_HOST } " >> " $PHP_ENV_CONFIG " ;
2016-10-07 02:46:20 +02:00
echo "Added ENV to php:"
2016-10-08 21:56:06 +02:00
cat " $PHP_ENV_CONFIG "
2016-10-07 02:46:20 +02:00
}
2017-02-09 05:46:46 +01:00
setup_web_password( ) {
if [ -z " ${ WEBPASSWORD +x } " ] ; then
# Not set at all, give the user a random pass
WEBPASSWORD = $( tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8)
echo " Assigning random password: $WEBPASSWORD "
fi ;
2017-05-05 05:37:26 +02:00
set -x
2017-05-09 22:03:03 +02:00
if [ [ " $WEBPASSWORD " = = "" ] ] ; then
echo "" | pihole -a -p
else
pihole -a -p " $WEBPASSWORD " " $WEBPASSWORD "
fi
2017-05-05 05:37:26 +02:00
{ set +x; } 2>/dev/null
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"
if [ " $IPv6 " != "True" ] ; then
ip_versions = "IPv4"
2017-10-04 18:05:27 +02:00
case $TAG in
2016-10-21 05:36:55 +02:00
"debian" ) sed -i '/use-ipv6.pl/ d' /etc/lighttpd/lighttpd.conf ; ;
2017-05-14 00:03:24 +02:00
"alpine" ) sed -i '/listen \[::\]:80/ d' /etc/nginx/nginx.conf ; ;
2016-10-21 05:36:55 +02:00
esac
fi ;
echo " Using $ip_versions "
}
2016-10-07 02:46:20 +02:00
test_configs( ) {
2017-10-04 18:05:27 +02:00
case $TAG in
2016-10-07 02:46:20 +02:00
"debian" ) test_configs_debian ; ;
"alpine" ) test_configs_alpine ; ;
esac
}
test_configs_debian( ) {
2016-10-07 19:14:12 +02:00
set -e
echo -n '::: Testing DNSmasq config: '
dnsmasq --test -7 /etc/dnsmasq.d
echo -n '::: Testing lighttpd config: '
lighttpd -t -f /etc/lighttpd/lighttpd.conf
set +e
echo "::: All config checks passed, starting ..."
2016-10-07 02:46:20 +02:00
}
test_configs_alpine( ) {
2016-10-07 19:14:12 +02:00
set -e
echo -n '::: Testing DNSmasq config: '
dnsmasq --test -7 /etc/dnsmasq.d
echo -n '::: Testing PHP-FPM config: '
2017-05-14 00:03:24 +02:00
php-fpm5 -t
2016-10-07 19:14:12 +02:00
echo -n '::: Testing NGINX config: '
nginx -t
set +e
echo "::: All config checks passed, starting ..."
2016-10-07 02:46:20 +02:00
}
test_framework_stubbing( ) {
2017-05-14 00:03:24 +02:00
if [ -n " $PYTEST " ] ; then
echo ":::::: Tests are being ran - stub out ad list fetching and add a fake ad block"
sed -i 's/^gravity_spinup$/#gravity_spinup # DISABLED FOR PYTEST/g' " $( which gravity.sh) "
echo 'testblock.pi-hole.local' >> /etc/pihole/blacklist.txt
fi
2016-10-07 02:46:20 +02:00
}
2016-10-30 18:13:52 +01:00
2017-01-18 05:49:42 +01:00
docker_main( ) {
2017-01-08 05:50:39 +01:00
echo -n '::: Starting up DNS and Webserver ...'
2017-01-18 05:49:42 +01:00
service dnsmasq restart # Just get DNS up. The webserver is down!!!
2017-01-08 05:50:39 +01:00
2017-10-04 18:05:27 +02:00
TAG = " $1 "
case $TAG in # Setup webserver
2016-10-30 18:13:52 +01:00
"alpine" )
2017-05-15 20:40:44 +02:00
php-fpm5
2017-01-08 05:50:39 +01:00
nginx
2016-10-30 18:13:52 +01:00
; ;
"debian" )
2017-01-08 05:50:39 +01:00
service lighttpd start
2016-10-30 18:13:52 +01:00
; ;
esac
2017-01-08 05:50:39 +01:00
gravity.sh # Finally lets update and be awesome.
2016-11-06 20:41:14 +01:00
tail -F " ${ WEBLOGDIR } " /*.log /var/log/pihole.log
2016-10-30 18:13:52 +01:00
}