dnsmasq_regenconf_hook 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. source /usr/share/yunohost/helpers
  3. force=${2:-0} # 0/1 --force argument
  4. dryrun=${3:-0} # 0/1 --dry-run argument
  5. pending_conf=$4 # Path of the pending conf file
  6. app="__APP__"
  7. do_pre_regen() {
  8. if [ $dryrun -eq 0 ]
  9. then
  10. dnsmasq_dir="${pending_conf}/etc/dnsmasq.d"
  11. mkdir -p "$dnsmasq_dir"
  12. cp -a "/etc/dnsmasq.conf" "${pending_conf}/etc/dnsmasq.conf"
  13. # Créer une sauvegarde des config dnsmasq de pi-hole. Que la regen-conf va sauvagement supprimer
  14. cp -a "/etc/.pihole/advanced/01-pihole.conf" "$dnsmasq_dir/"
  15. ynh_replace_string --match_string="@DNS1@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf"
  16. ynh_replace_string --match_string="@DNS2@" --replace_string="" --target_file="$dnsmasq_dir/01-pihole.conf"
  17. ynh_replace_string --match_string="^no-resolv" --replace_string="#no-resolv" --target_file="$dnsmasq_dir/01-pihole.conf"
  18. ynh_replace_string --match_string="@INT@" --replace_string="$main_iface" --target_file="$dnsmasq_dir/01-pihole.conf"
  19. if [ "$query_logging" = "true" ]; then
  20. ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  21. else
  22. ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  23. fi
  24. # Fix a too recent option for our dnsmasq version.
  25. ynh_replace_string --match_string="log-queries=extra" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  26. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  27. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  28. if [ $enable_dhcp -eq 1 ]
  29. then
  30. # Get the default network interface
  31. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  32. # Find the IP associated to the network interface
  33. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  34. max_dhcp_range=250
  35. dhcp_range=100
  36. # Define the dhcp range from the current ip
  37. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  38. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  39. b_range=$(( $ip_fourth_part + $dhcp_range ))
  40. if [ $b_range -gt $max_dhcp_range ]; then
  41. b_range=$max_dhcp_range
  42. fi
  43. a_range=$(( $b_range - $dhcp_range ))
  44. # Get the gateway
  45. gateway=$(ip route | grep default | awk '{print $3;}')
  46. # And the mac adress
  47. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  48. # Copy the config file
  49. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  50. # And set the config
  51. 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"
  52. 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"
  53. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  54. # Set a static ip for the server.
  55. echo "dhcp-host=$hw_adress,$localipv4" > "${pending_conf}/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  56. fi
  57. fi
  58. }
  59. do_post_regen() {
  60. # Reload dnsmasq
  61. systemctl reload dnsmasq
  62. }
  63. case "$1" in
  64. pre)
  65. do_pre_regen
  66. ;;
  67. post)
  68. do_post_regen
  69. ;;
  70. *)
  71. echo "Hook called with unknown argument \`$1'" >&2
  72. exit 1
  73. ;;
  74. esac
  75. exit 0