diff --git a/actions.toml b/actions.toml index fc7f8aa..71fd449 100644 --- a/actions.toml +++ b/actions.toml @@ -16,16 +16,6 @@ command = "/bin/bash scripts/actions/reset_default_config \"pihole-FTL.conf\"" accepted_return_codes = [0] description = "Reset the config file pihole-FTL.conf." -[reset_default_dnsmasq] -name = "Reset the config file and restore a default one." -command = "/bin/bash scripts/actions/reset_default_config \"01-pihole.conf\"" -# user = "root" # optional -# cwd = "/" # optional -# accepted_return_codes = [0, 1, 2, 3] # optional -accepted_return_codes = [0] -description = "Reset the config file dnsmasq.d/01-pihole.conf." - - [reset_default_nginx] name = "Reset the nginx config for this app." command = "/bin/bash scripts/actions/reset_default_system nginx" diff --git a/conf/dnsmasq_regenconf_hook b/conf/dnsmasq_regenconf_hook index 0e4c2ef..bc8fd2a 100755 --- a/conf/dnsmasq_regenconf_hook +++ b/conf/dnsmasq_regenconf_hook @@ -1,53 +1,79 @@ #!/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 +source /usr/share/yunohost/helpers -temp_dir=/tmp/pi-hole.bck +app="__APP__" -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" +action=$1 +pending_conf=$4/../dnsmasq - # Décommente le cache-size de la config par défaut - sed --in-place "s/^#pihole# cache-size=/cache-size=/g" /etc/dnsmasq.conf - # Et commente celui de pi-hole - sed --in-place "s/^cache-size=/#cache-size=/g" /etc/dnsmasq.d/01-pihole.conf +[[ "$action" == "pre" ]] || exit 0 +[[ -d "$pending_conf" ]] || exit 0 + +# +# Regen /etc/dnsmasq.d/01-pihole.conf +# +dnsmasq_dir="${pending_conf}/etc/dnsmasq.d" +mkdir -p "$dnsmasq_dir" + +cp -a "/etc/.pihole/advanced/01-pihole.conf" "$dnsmasq_dir/" + +ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" +ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf" +ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file="$dnsmasq_dir/01-pihole.conf" +ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file="$dnsmasq_dir/01-pihole.conf" +query_logging=$(ynh_app_setting_get --app=$app --key=query_logging) +if [ "$query_logging" = "true" ]; then + ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" +else + ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf" +fi + +# +# Tweak dnsmsasq's general conf cache-size +# + +ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf" + +# +# Regen /etc/dnsmasq.d/02-pihole-dhcp.conf +# + +enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp) +if [ $enable_dhcp -eq 1 ] +then + + # Get the default network interface + main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}') + # Find the IP associated to the network interface + localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1) + + 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 )) -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 + # Get the gateway + gateway=$(ip route | grep default | awk '{print $3;}') + # And the mac adress + hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}') - # Commente le cache-size de la config par défaut - sed --in-place "s/^cache-size=/#pihole# cache-size=/g" /etc/dnsmasq.conf + # Copy the config file + cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/" - # 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 + # And set the config + ynh_replace_string --match_string="__A_RANGE__" --replace_string="$ip_beginning_part.$a_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + ynh_replace_string --match_string="__B_RANGE__" --replace_string="$ip_beginning_part.$b_range" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf" + # Set a static ip for the server. + echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf" +fi exit 0 diff --git a/scripts/actions/reset_default_app b/scripts/actions/reset_default_app index 8e3fba5..adbebb1 100755 --- a/scripts/actions/reset_default_app +++ b/scripts/actions/reset_default_app @@ -31,6 +31,7 @@ app=$YNH_APP_INSTANCE_NAME path_url=$(ynh_app_setting_get --app=$app --key=path) domain=$(ynh_app_setting_get --app=$app --key=domain) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) pihole_version="$(ynh_app_setting_get --app=$app --key=pihole_version)" #================================================= @@ -228,7 +229,11 @@ ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --ta # REINSTALL CONF_REGEN HOOK #================================================= -(cd scripts; cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app) +( + cd scripts + cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app + ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" +) #================================================= # RESTART PIHOLE-FTL diff --git a/scripts/actions/reset_default_config b/scripts/actions/reset_default_config index fcec8b7..c3b6fe4 100755 --- a/scripts/actions/reset_default_config +++ b/scripts/actions/reset_default_config @@ -34,8 +34,6 @@ if [ "$file" = "setupVars.conf" ]; then config_file="/etc/pihole/setupVars.conf" elif [ "$file" = "pihole-FTL.conf" ]; then config_file="/etc/pihole/pihole-FTL.conf" -elif [ "$file" = "01-pihole.conf" ]; then - config_file="/etc/dnsmasq.d/01-pihole.conf" fi #================================================= @@ -77,27 +75,6 @@ then # Restart pihole-FTL ynh_systemd_action --action=restart --service_name=pihole-FTL - -elif [ "$file" = "01-pihole.conf" ] -then - cp "$pihole_local_repo/advanced/01-pihole.conf" $config_file - # Use dns from /etc/resolv.dnsmasq.conf - ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file=$config_file - ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file=$config_file - ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file=$config_file - - ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file=$config_file - if [ "$query_logging" = "true" ]; then - ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file=$config_file - else - ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file=$config_file - fi - - # Fix a too recent option for our dnsmasq version. - ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file=$config_file - - # To prevent any conflict with the original dnsmasq config, comment cache-size in the original config. - ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf fi # Calculate and store the config file checksum into the app settings diff --git a/scripts/backup b/scripts/backup index 2202c71..0646942 100644 --- a/scripts/backup +++ b/scripts/backup @@ -77,16 +77,9 @@ ynh_backup --src_path="/etc/sudoers.d/pihole" ynh_backup --src_path="/etc/init.d/pihole-FTL" ynh_backup --src_path="/usr/bin/pihole-FTL" -ynh_backup --src_path="/etc/dnsmasq.d/01-pihole.conf" -if test -e "/etc/dnsmasq.d/02-pihole-dhcp.conf"; then - ynh_backup --src_path="/etc/dnsmasq.d/02-pihole-dhcp.conf" -fi if test -e "/etc/dnsmasq.d/03-pihole-wildcard.conf"; then ynh_backup --src_path="/etc/dnsmasq.d/03-pihole-wildcard.conf" fi -if test -e "/etc/dnsmasq.d/04-pihole-static-dhcp.conf"; then - ynh_backup --src_path="/etc/dnsmasq.d/04-pihole-static-dhcp.conf" -fi ynh_backup --src_path="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" diff --git a/scripts/install b/scripts/install index 9c3c494..e1197f0 100644 --- a/scripts/install +++ b/scripts/install @@ -260,7 +260,7 @@ else # Replace the service dnsmasq by pihole-FTL # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL - ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service + ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service # Reload systemd config systemctl daemon-reload @@ -290,36 +290,6 @@ echo "INSTALL_WEB=true" >> $setupVars # Calculate and store the config file checksum into the app settings ynh_store_file_checksum --file="$setupVars" -#================================================= -# SET UP DNSMASQ CONFIG -#================================================= -ynh_script_progression --message="Setting up Dnsmasq config..." --weight=2 - -# ynh_systemd_action --action=stop --service_name=dnsmasq - -pihole_dnsmasq_config="/etc/dnsmasq.d/01-pihole.conf" -cp "$pihole_local_repo/advanced/01-pihole.conf" $pihole_dnsmasq_config -# Use dns from /etc/resolv.dnsmasq.conf -ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file=$pihole_dnsmasq_config -ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file=$pihole_dnsmasq_config -ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file=$pihole_dnsmasq_config - -ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file=$pihole_dnsmasq_config -if [ "$query_logging" = "true" ]; then - ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file=$pihole_dnsmasq_config -else - ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file=$pihole_dnsmasq_config -fi - -# Fix a too recent option for our dnsmasq version. -ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file=$pihole_dnsmasq_config - -# Calculate and store the config file checksum into the app settings -ynh_store_file_checksum --file="$pihole_dnsmasq_config" - -# To prevent any conflict with the original dnsmasq config, comment cache-size in the original config. -ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf - #================================================= # CONFIGURE DNS FOR THE LOCAL DOMAINS #================================================= @@ -343,37 +313,6 @@ done <<< "$(yunohost domain list | grep "\." | sed 's/.*: \|.*- //')" # ENABLE DHCP SERVER #================================================= -if [ $enable_dhcp -eq 1 ] -then - ynh_script_progression --message="Enabling dhcp server..." - 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=$(ip route | grep default | awk '{print $3;}') - # And the mac adress - hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}') - - # Copy the config file - cp "../conf/02-pihole-dhcp.conf" "/etc/dnsmasq.d/" - - # And set the config - ynh_replace_string --match_string="__A_RANGE__" --replace_string="$ip_beginning_part.$a_range" --target_file="/etc/dnsmasq.d/02-pihole-dhcp.conf" - ynh_replace_string --match_string="__B_RANGE__" --replace_string="$ip_beginning_part.$b_range" --target_file="/etc/dnsmasq.d/02-pihole-dhcp.conf" - ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="/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 @@ -408,16 +347,10 @@ then ynh_script_progression --message="Restarting Dnsmasq..." --weight=2 ynh_systemd_action --action=restart --service_name=dnsmasq + + ynh_systemd_action --action=restart --service_name=pihole-FTL fi -#================================================= -# START PIHOLE-FTL -#================================================= - -ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2 - -ynh_systemd_action --action=restart --service_name=pihole-FTL - #================================================= # BUILD THE LISTS WITH GRAVITY #================================================= @@ -436,6 +369,18 @@ ynh_exec_warn_less /opt/pihole/gravity.sh #================================================= cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app +ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" + +systemctl daemon-reload +ynh_exec_warn_less yunohost tools regen-conf dnsmasq + +#================================================= +# START PIHOLE-FTL +#================================================= + +ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2 + +ynh_systemd_action --action=restart --service_name=pihole-FTL #================================================= # GENERIC FINALISATION diff --git a/scripts/remove b/scripts/remove index b9e9537..0870673 100755 --- a/scripts/remove +++ b/scripts/remove @@ -54,7 +54,7 @@ else fi # Move back the service configuration for dnsmasq - ynh_secure_remove --file="/etc/systemd/system/multi-user.target.wants/dnsmasq.service" + ynh_secure_remove --file="/etc/systemd/system/dnsmasq.service" mv /lib/systemd/system/.dnsmasq.service.backup_by_pihole /lib/systemd/system/dnsmasq.service mv /etc/init.d/.dnsmasq.backup_by_pihole /etc/init.d/dnsmasq @@ -151,13 +151,7 @@ ynh_secure_remove --file="/etc/sudoers.d/pihole" #================================================= ynh_script_progression --message="Removing Dnsmasq config..." --weight=2 -ynh_systemd_action --action=stop --service_name=dnsmasq -ynh_secure_remove --file="/etc/dnsmasq.d/01-pihole.conf" -ynh_secure_remove --file="/etc/dnsmasq.d/02-pihole-dhcp.conf" ynh_secure_remove --file="/etc/dnsmasq.d/03-pihole-wildcard.conf" -ynh_secure_remove --file="/etc/dnsmasq.d/04-pihole-static-dhcp.conf" - -ynh_replace_string --match_string="#pihole# " --replace_string="" --target_file=/etc/dnsmasq.conf #================================================= # CLEAN /etc/hosts @@ -170,25 +164,23 @@ ynh_replace_string --match_string="#Commented by pihole# " --replace_string="" - # And remove extra lines, added by PiHole sed -i "/#Added by pihole#/d" /etc/hosts +#================================================= +# REMOVE CONF_REGEN HOOK +#================================================= + +ynh_systemd_action --action=stop --service_name=dnsmasq + +ynh_secure_remove --file=/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app + +ynh_exec_warn_less yunohost tools regen-conf dnsmasq + #================================================= # RESTART DNSMASQ #================================================= ynh_script_progression --message="Restarting Dnsmasq..." -if [ "$pihole_version" == "Last available" ] -then - # Quietly start dnsmasq a first time, because it usually doesn't start correctly the first time. - ynh_exec_fully_quiet systemctl start dnsmasq - sleep 1 -fi ynh_systemd_action --action=restart --service_name=dnsmasq -#================================================= -# REMOVE CONF_REGEN HOOK -#================================================= - -ynh_secure_remove --file=/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app - #================================================= # GENERIC FINALISATION #================================================= @@ -196,6 +188,11 @@ ynh_secure_remove --file=/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app #================================================= ynh_script_progression --message="Removing the dedicated system user..." --weight=2 +if [ "$pihole_version" == "Last 3.X" ] +then + # Dirty hack to remove correctly the user + killall -u $app +fi ynh_system_user_delete --username=$app #================================================= diff --git a/scripts/restore b/scripts/restore index 2cf05da..68981e9 100644 --- a/scripts/restore +++ b/scripts/restore @@ -182,7 +182,7 @@ then # Replace the service dnsmasq by pihole-FTL # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL - ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service + ln -s /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service # Reload systemd config systemctl daemon-reload @@ -193,18 +193,11 @@ fi #================================================= ynh_script_progression --message="Restoring Dnsmasq config..." -ynh_systemd_action --action=stop --service_name=dnsmasq - -ynh_restore_file --origin_path="/etc/dnsmasq.d/01-pihole.conf" -test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/02-pihole-dhcp.conf" && \ - ynh_restore_file --origin_path="/etc/dnsmasq.d/02-pihole-dhcp.conf" test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/03-pihole-wildcard.conf" && \ ynh_restore_file --origin_path="/etc/dnsmasq.d/03-pihole-wildcard.conf" -test -e "${YNH_APP_BACKUP_DIR}/etc/dnsmasq.d/04-pihole-static-dhcp.conf" && \ - ynh_restore_file --origin_path="/etc/dnsmasq.d/04-pihole-static-dhcp.conf" -# To prevent any conflict with the original dnsmasq config, comment cache-size in the original config. -ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file=/etc/dnsmasq.conf +systemctl daemon-reload +ynh_exec_warn_less yunohost tools regen-conf dnsmasq #================================================= # CONFIGURE DNS FOR THE LOCAL DOMAINS diff --git a/scripts/upgrade b/scripts/upgrade index eea787a..17e741e 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -305,7 +305,7 @@ else # Replace the service dnsmasq by pihole-FTL # That way, YunoHost can continue to use dnsmasq by actually using pihole-FTL - ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/multi-user.target.wants/dnsmasq.service + ln -sf /run/systemd/generator.late/pihole-FTL.service /etc/systemd/system/dnsmasq.service # Reload systemd config systemctl daemon-reload @@ -355,13 +355,6 @@ fi # Remove git usage for version. Which fails because we use here a release instead of master. ynh_replace_string --match_string=".*updatechecker.*" --replace_string="#&" --target_file=/etc/cron.d/pihole -#================================================= -# START PIHOLE-FTL -#================================================= -ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2 - -ynh_systemd_action --action=restart --service_name=pihole-FTL - #================================================= # ADVERTISE SERVICE IN ADMIN PANEL #================================================= @@ -373,6 +366,17 @@ yunohost service add pihole-FTL --description="PiHole backend service" --log="/v #================================================= cp ../conf/dnsmasq_regenconf_hook /usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app +ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="/usr/share/yunohost/hooks/conf_regen/50-dnsmasq_$app" + +systemctl daemon-reload +ynh_exec_warn_less yunohost tools regen-conf dnsmasq + +#================================================= +# START PIHOLE-FTL +#================================================= +ynh_script_progression --message="Restarting PiHole-FTL..." --weight=2 + +ynh_systemd_action --action=restart --service_name=pihole-FTL #================================================= # RELOAD NGINX