dnsmasq_regenconf_hook 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/dnsmasq.d/01-pihole.conf" "$dnsmasq_dir/"
  15. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  16. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  17. if [ $enable_dhcp -eq 1 ]
  18. then
  19. # Get the default network interface
  20. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  21. # Find the IP associated to the network interface
  22. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  23. max_dhcp_range=250
  24. dhcp_range=100
  25. # Define the dhcp range from the current ip
  26. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  27. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  28. b_range=$(( $ip_fourth_part + $dhcp_range ))
  29. if [ $b_range -gt $max_dhcp_range ]; then
  30. b_range=$max_dhcp_range
  31. fi
  32. a_range=$(( $b_range - $dhcp_range ))
  33. # Get the gateway
  34. gateway=$(ip route | grep default | awk '{print $3;}')
  35. # And the mac adress
  36. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  37. # Copy the config file
  38. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  39. # And set the config
  40. 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"
  41. 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"
  42. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  43. # Set a static ip for the server.
  44. echo "dhcp-host=$hw_adress,$localipv4" > "${pending_conf}/etc/dnsmasq.d/04-pihole-static-dhcp.conf"
  45. fi
  46. fi
  47. }
  48. do_post_regen() {
  49. # Reload dnsmasq
  50. systemctl reload dnsmasq
  51. }
  52. case "$1" in
  53. pre)
  54. do_pre_regen
  55. ;;
  56. post)
  57. do_post_regen
  58. ;;
  59. *)
  60. echo "Hook called with unknown argument \`$1'" >&2
  61. exit 1
  62. ;;
  63. esac
  64. exit 0