1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/pihole_ynh.git synced 2024-09-03 20:05:58 +02:00
pihole_ynh/scripts/install
2018-05-20 22:19:05 +02:00

375 lines
13 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
# Load common variables for all scripts.
source _variables
#=================================================
# MANAGE FAILURE OF THE SCRIPT
#=================================================
ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée.
#=================================================
# RETRIEVE ARGUMENTS FROM THE MANIFEST
#=================================================
domain=$YNH_APP_ARG_DOMAIN
path_url=$YNH_APP_ARG_PATH
admin=$YNH_APP_ARG_ADMIN
query_logging=$YNH_APP_ARG_QUERY_LOGGING
enable_dhcp=$YNH_APP_ARG_ENABLE_DHCP
app=$YNH_APP_INSTANCE_NAME
#=================================================
# CHECK IF THE APP CAN BE INSTALLED WITH THIS ARGS
#=================================================
final_path=/var/www/$app
test ! -e "$final_path" || ynh_die "This path already contains a folder"
# Normalize the url path syntax
path_url=$(ynh_normalize_url_path $path_url)
# Check web path availability
ynh_webpath_available $domain $path_url
# Register (book) web path
ynh_webpath_register $app $domain $path_url
#=================================================
# STORE SETTINGS FROM MANIFEST
#=================================================
ynh_app_setting_set $app domain $domain
ynh_app_setting_set $app path $path_url
ynh_app_setting_set $app admin $admin
ynh_app_setting_set $app query_logging $query_logging
ynh_app_setting_set $app enable_dhcp $enable_dhcp
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_ON
#=================================================
# STANDARD MODIFICATIONS
#=================================================
# FIND AND OPEN A PORT
#=================================================
port=$(ynh_find_port 4711) # Cherche un port libre.
if [ $port -gt 4720 ]
then
ynh_die "The ports 4711 to 4720 are already in use. Pi-hole can't works on another port. Please try to free one of this ports."
fi
# Ouvre le port dans le firewall
ynh_exec_fully_quiet yunohost firewall allow --no-upnp TCP $port
ynh_app_setting_set $app port $port
# Désactive le port 53 en upnp
ynh_exec_fully_quiet yunohost firewall disallow Both 53 --no-reload
ynh_exec_fully_quiet yunohost firewall allow Both 53 --no-upnp
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_install_app_dependencies $app_depencencies
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_app_setting_set $app final_path $final_path
# Créer une copie du repo de pihole (nécessaire pour Gravity)
pihole_local_repo="/etc/.pihole"
ynh_setup_source "$pihole_local_repo"
# Installe le dashboard admin
ynh_setup_source "$final_path" admin_dashboard
#=================================================
# NGINX CONFIGURATION
#=================================================
if [ "$path_url" != "/" ]
then
ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf"
fi
ynh_add_nginx_config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_system_user_create $app # Créer un utilisateur système dédié à l'app
#=================================================
# PHP-FPM CONFIGURATION
#=================================================
ynh_add_fpm_config # Créer le fichier de configuration du pool php-fpm et le configure.
#=================================================
# SPECIFIC SETUP
#=================================================
# CREATE THE DIRECTORIES AND POPULATE THEM
#=================================================
pihole_storage="/etc/pihole"
mkdir -p "$pihole_storage"
chown $app: -R "$pihole_storage"
pihole_dir="/opt/pihole"
mkdir -p "$pihole_dir"
# Copie les scripts de Pi-hole
cp -a "$pihole_local_repo/gravity.sh" "$pihole_dir/"
cp -a $pihole_local_repo/advanced/Scripts/*.sh "$pihole_dir/"
# And copy this fucking COL_TABLE file...
cp -a "$pihole_local_repo/advanced/Scripts/COL_TABLE" "$pihole_dir/"
#=================================================
# COPY THE PI-HOLE MAIN SCRIPT
#=================================================
cp -a "$pihole_local_repo/pihole" /usr/local/bin/
cp -a "$pihole_local_repo/advanced/bash-completion/pihole" /etc/bash_completion.d/pihole
#=================================================
# CREATE LOG FILES
#=================================================
touch /var/log/pihole.log
chmod 644 /var/log/pihole.log
dnsmasq_user=$(grep DNSMASQ_USER= /etc/init.d/dnsmasq | cut -d'"' -f2)
chown $dnsmasq_user:root /var/log/pihole.log
#=================================================
# CREATE SUDOER FILE
#=================================================
# Cette configuration sudoers autorise pihole à exécuter /usr/local/bin/pihole en root sans mot de passe. Pas plus.
cp "$pihole_local_repo/advanced/pihole.sudo" /etc/sudoers.d/pihole
echo "$app ALL=NOPASSWD: /usr/local/bin/pihole" >> /etc/sudoers.d/pihole
# echo "Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin" >> /etc/sudoers.d/pihole
chmod 0440 /etc/sudoers.d/pihole
#=================================================
# INSTALL LOGROTATE SCRIPT FOR PI-HOLE
#=================================================
cp "$pihole_local_repo/advanced/logrotate" "$pihole_storage/logrotate"
sed -i "/# su #/d;" "$pihole_storage/logrotate"
#=================================================
# INSTALLATION OF PIHOLE-FTL
#=================================================
# Get the source of Pi-Hole-FTL
FTL_temp_path=$(mktemp -d)
ynh_setup_source "$FTL_temp_path" FTL
# Plutôt que télécharger le binaire C, on le compile nous-même.
( cd "$FTL_temp_path"
ynh_exec_warn_less make
ynh_exec_warn_less make install )
ynh_secure_remove "$FTL_temp_path"
cp "../conf/pihole-FTL.conf" "$pihole_storage"
cp -a $pihole_local_repo/advanced/pihole-FTL.service /etc/init.d/pihole-FTL
chmod +x /etc/init.d/pihole-FTL
ynh_exec_warn_less systemctl enable pihole-FTL
#=================================================
# BUILD THE VARIABLES FILE
#=================================================
setupVars="$pihole_storage/setupVars.conf"
# Trouve l'interface réseau par défaut
main_iface=$(route | grep default | awk '{print $8;}' | head -n1)
echo "PIHOLE_INTERFACE=$main_iface" > $setupVars
echo "IPV4_ADDRESS=127.0.0.1" >> $setupVars
echo "IPV6_ADDRESS=" >> $setupVars
echo "PIHOLE_DNS_1=" >> $setupVars
echo "PIHOLE_DNS_2=" >> $setupVars
if [ $query_logging -eq 1 ]; then
query_logging=true
else
query_logging=false
fi
echo "QUERY_LOGGING=$query_logging" >> $setupVars
echo "INSTALL_WEB=true" >> $setupVars
ynh_store_file_checksum "$setupVars" # Enregistre la somme de contrôle du fichier de config
#=================================================
# SET UP THE DNSMASQ CONFIG
#=================================================
ynh_system_reload dnsmasq stop
pihole_dnsmasq_config="/etc/dnsmasq.d/01-pihole.conf"
cp "$pihole_local_repo/advanced/01-pihole.conf" $pihole_dnsmasq_config
# On utilise les dns de /etc/resolv.dnsmasq.conf
ynh_replace_string "@DNS1@" "" $pihole_dnsmasq_config
ynh_replace_string "@DNS2@" "" $pihole_dnsmasq_config
ynh_replace_string "^no-resolv" "#no-resolv" $pihole_dnsmasq_config
ynh_replace_string "@INT@" "$main_iface" $pihole_dnsmasq_config
if [ "$query_logging" = "true" ]; then
ynh_replace_string "^#log-queries" "log-queries" $pihole_dnsmasq_config
else
ynh_replace_string "^log-queries" "#log-queries" $pihole_dnsmasq_config
fi
# Fix a too recent option for our dnsmasq version.
ynh_replace_string "log-queries=extra" "log-queries" $pihole_dnsmasq_config
ynh_store_file_checksum "$pihole_dnsmasq_config" # Enregistre la somme de contrôle du fichier de config
# Pour éviter un conflit entre les config de dnsmasq, il faut commenter cache-size dans la config par défaut.
ynh_replace_string "^cache-size=" "#pihole# cache-size=" /etc/dnsmasq.conf
#=================================================
# CONFIGURE DNS FOR THE LOCAL DOMAINS
#=================================================
# Trouve l'ipv4 associée à l'interface trouvée
localipv4=$(ifconfig | grep -A 1 "$main_iface" | tail -1 | awk '{print $2;}' | cut -d: -f2)
# Liste les domaines de yunohost
while read perdomain
do
# Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local
ynh_replace_string "^127.0.0.1.*$perdomain" "#Commented by pihole# &" /etc/hosts
# Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà
grep -q "^$localipv4.*$perdomain" /etc/hosts || \
echo "$localipv4 $perdomain #Added by pihole#" >> /etc/hosts
done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')"
#=================================================
# ENABLE DHCP SERVER
#=================================================
if [ $enable_dhcp -eq 1 ]
then
max_dhcp_range=250
dhcp_range=100
# Define the dhcp range from the current ip
ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
b_range=$(( $ip_fourth_part + $dhcp_range ))
if [ $b_range -gt $max_dhcp_range ]; then
b_range=$max_dhcp_range
fi
a_range=$(( $b_range - $dhcp_range ))
# Get the gateway
gateway=$(route | grep default | awk '{print $2;}' | head -n1)
# And the mac adress
hw_adress=$(ifconfig | grep "eth0" | awk '{print $5;}')
# Copy the config file
cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/"
# And set the config
ynh_replace_string "__A_RANGE__" "$ip_beginning_part.$a_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
ynh_replace_string "__B_RANGE__" "$ip_beginning_part.$b_range" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
ynh_replace_string "__GATEWAY__" "$gateway" "/etc/dnsmasq.d/02-pihole-dhcp.conf"
# Set a static ip for the server.
echo "dhcp-host=$hw_adress,$localipv4" > "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
fi
# Open the UDP port 67 for dhcp
ynh_exec_fully_quiet yunohost firewall allow UDP 67 --no-upnp
#=================================================
# RESTART DNSMASQ
#=================================================
ynh_system_reload dnsmasq restart
#=================================================
# INSTALL THE CRON JOB
#=================================================
cp $pihole_local_repo/advanced/pihole.cron /etc/cron.d/pihole
# Remove git usage for version. Which fails because we use here a release instead of master.
ynh_replace_string ".*updatechecker.*" "#&" /etc/cron.d/pihole
#=================================================
# BUILD THE LISTS WITH GRAVITY
#=================================================
cp "$pihole_local_repo/adlists.default" "$pihole_storage/adlists.default"
ynh_exec_warn_less /opt/pihole/gravity.sh
#=================================================
# START PIHOLE-FTL
#=================================================
ynh_system_reload pihole-FTL restart
#=================================================
# SET UP THE CONF_REGEN HOOK
#=================================================
cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app
#=================================================
# GENERIC FINALISATION
#=================================================
# ENABLE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add pihole-FTL --log "/var/log/pihole-FTL.log"
#=================================================
# RESTRAIN THE ACCESS TO THE ADMIN ONLY
#=================================================
yunohost app addaccess --users=$admin $app
#=================================================
# RELOAD NGINX
#=================================================
ynh_system_reload nginx
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_OFF
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
if [ $enable_dhcp -eq 1 ]
then
dhcp_alert="You asked to use the internal DHCP server of dnsmasq with PiHole.
You should really read the documentation about that, https://github.com/YunoHost-Apps/pihole_ynh/blob/master/dhcp.md
"
else
dhcp_alert=""
fi
message="${dhcp_alert}If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/pihole_ynh"
ynh_send_readme_to_admin "$message" "$admin"