From d66ce429eb151924c9aed7edc2b52ad173fcbb1c Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Fri, 11 Aug 2017 00:56:26 +0200 Subject: [PATCH] Ajouts hooks domain et conf_regen --- conf/dnsmasq_regenconf_hook | 48 +++++++++++++++++++++++++++++++++++++ hooks/post_domain_add | 15 ++++++++++++ hooks/post_domain_remove | 10 ++++++++ scripts/backup | 2 ++ scripts/install | 6 +++++ scripts/remove | 8 ++++++- scripts/restore | 2 ++ scripts/upgrade | 6 +++++ 8 files changed, 96 insertions(+), 1 deletion(-) create mode 100755 conf/dnsmasq_regenconf_hook create mode 100644 hooks/post_domain_add create mode 100644 hooks/post_domain_remove diff --git a/conf/dnsmasq_regenconf_hook b/conf/dnsmasq_regenconf_hook new file mode 100755 index 0000000..1a2cf59 --- /dev/null +++ b/conf/dnsmasq_regenconf_hook @@ -0,0 +1,48 @@ +#!/bin/bash + +force=${2:-0} # 0/1 --force argument +dryrun=${3:-0} # 0/1 --dry-run argument +pending_conf=$4 # Path of the pending conf file + +temp_dir=/tmp/pi-hole.bck + +do_pre_regen() { + if [ $dryrun -eq 0 ] + then + # Créer une sauvegarde des config dnsmasq de pi-hole. Que la regen-conf va sauvagement supprimer + mkdir $temp_dir + cp -a "/etc/dnsmasq.d/01-pihole.conf" "$temp_dir" + test -e "/etc/dnsmasq.d/02-pihole-dhcp.conf" && cp -a "/etc/dnsmasq.d/02-pihole-dhcp.conf" "$temp_dir" + test -e "/etc/dnsmasq.d/03-pihole-wildcard.conf" && cp -a "/etc/dnsmasq.d/03-pihole-wildcard.conf" "$temp_dir" + fi +} + +do_post_regen() { + # Restaure la config dnsmasq de pi-hole + cp -a "$temp_dir/01-pihole.conf" "/etc/dnsmasq.d/" + test -e "$temp_dir/02-pihole-dhcp.conf" && cp -a "$temp_dir/02-pihole-dhcp.conf" "/etc/dnsmasq.d/" + test -e "$temp_dir/03-pihole-wildcard.conf" && cp -a "$temp_dir/03-pihole-wildcard.conf" "/etc/dnsmasq.d/" + # Supprime le dossier temporaire + test -n $temp_dir && rm -r $temp_dir + + # Commente le cache-size de la config par défaut + sed --in-place "s/^cache-size=/#pihole# cache-size=/g" /etc/dnsmasq.conf + + # Reload dnsmasq + systemctl reload dnsmasq +} + +case "$1" in + pre) + do_pre_regen + ;; + post) + do_post_regen + ;; + *) + echo "Hook called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/hooks/post_domain_add b/hooks/post_domain_add new file mode 100644 index 0000000..2dda9e7 --- /dev/null +++ b/hooks/post_domain_add @@ -0,0 +1,15 @@ +#!/bin/bash + +domain=$1 + +# Trouve l'interface réseau par défaut +main_iface=$(route | grep default | awk '{print $8;}' | head -n1) +# Trouve l'ipv4 associée à l'interface trouvée +localipv4=$(ifconfig | grep -A 1 "$main_iface" | tail -1 | awk '{print $2;}' | cut -d: -f2) + +# Commente les résolutions du domaine sur 127.0.0.1, qui risquerait de bloquer la résolution sur le réseau local +sed -i "s/^127.0.0.1.*$domain/#Commented by pihole# &/g" /etc/hosts + +# Et ajoute une résolution sur l'ip local à la place, si elle n'existe pas déjà +grep -q "^$localipv4.*$domain" /etc/hosts || \ + echo "$localipv4 $domain #Added by pihole#" >> /etc/hosts diff --git a/hooks/post_domain_remove b/hooks/post_domain_remove new file mode 100644 index 0000000..56fb422 --- /dev/null +++ b/hooks/post_domain_remove @@ -0,0 +1,10 @@ +#!/bin/bash + +domain=$1 + +# Restaure les résolutions qui avaient été commentées par pihole pour ce domaine +sed -i "s/#Commented by pihole# \(.*domain1\)/\1/g" /etc/hosts +# On utilise ici les sous-expressions sed pour supprimer seulement le début de la ligne. + +# Et supprime les lignes ajoutées par pihole pour ce domaine +sed -i "/$domain #Added by pihole#/d" /etc/hosts diff --git a/scripts/backup b/scripts/backup index 08c25ed..c397f7b 100644 --- a/scripts/backup +++ b/scripts/backup @@ -90,3 +90,5 @@ fi if test -e "/etc/dnsmasq.d/03-pihole-wildcard.conf"; then ynh_backup "/etc/dnsmasq.d/03-pihole-wildcard.conf" "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" fi + +ynh_backup "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" "${YNH_APP_BACKUP_DIR}/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" diff --git a/scripts/install b/scripts/install index 838f2fb..75c8f11 100644 --- a/scripts/install +++ b/scripts/install @@ -256,6 +256,12 @@ SUPPRESS_WARNING /opt/pihole/gravity.sh systemctl start pihole-FTL +#================================================= +# SET UP THE CONF_REGEN HOOK +#================================================= + +cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app + #================================================= # GENERIC FINALISATION #================================================= diff --git a/scripts/remove b/scripts/remove index 73ca6da..c348228 100755 --- a/scripts/remove +++ b/scripts/remove @@ -35,7 +35,7 @@ fi #================================================= systemctl stop pihole-FTL -systemctl disable pihole-FTL +SUPPRESS_WARNING systemctl disable pihole-FTL rm -f "/etc/init.d/pihole-FTL" "/usr/bin/pihole-FTL" "/var/run/pihole-FTL.pid" "/var/run/pihole-FTL.port" #================================================= @@ -125,6 +125,12 @@ sed -i "/#Added by pihole#/d" /etc/hosts systemctl start dnsmasq +#================================================= +# REMOVE THE CONF_REGEN HOOK +#================================================= + +rm /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app + #================================================= # GENERIC FINALISATION #================================================= diff --git a/scripts/restore b/scripts/restore index 46633f6..f20dba5 100644 --- a/scripts/restore +++ b/scripts/restore @@ -123,6 +123,8 @@ ynh_restore_file "/usr/bin/pihole-FTL" ynh_restore_file "/var/run/pihole-FTL.pid" ynh_restore_file "/var/run/pihole-FTL.port" +ynh_restore_file "/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" + #================================================= # RESTORE OF DNSMASQ CONFIG #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 7f2ffc9..b3f0fbf 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -164,6 +164,12 @@ systemctl start pihole-FTL systemctl start pihole-FTL +#================================================= +# UPDATE THE CONF_REGEN HOOK +#================================================= + +cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app + #================================================= # RELOAD NGINX #=================================================