dnsmasq_regenconf_hook 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. source /usr/share/yunohost/helpers
  3. app="__APP__"
  4. action=$1
  5. pending_conf=$4/../dnsmasq
  6. [[ "$action" == "pre" ]] || exit 0
  7. [[ -d "$pending_conf" ]] || exit 0
  8. #
  9. # Tweak dnsmsasq's general conf cache-size
  10. #
  11. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  12. ynh_replace_string --match_string="^listen-address=" --replace_string="#pihole# listen-address=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  13. echo "
  14. conf-dir=/etc/dnsmasq.d/" >> "${pending_conf}/etc/dnsmasq.conf"
  15. #
  16. # Regen /etc/dnsmasq.d/02-pihole-dhcp.conf
  17. #
  18. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  19. if [ $enable_dhcp -eq 1 ]
  20. then
  21. # Get the default network interface
  22. # Find the IP associated to the network interface
  23. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  24. max_dhcp_range=250
  25. dhcp_range=100
  26. # Define the dhcp range from the current ip
  27. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  28. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  29. b_range=$(( $ip_fourth_part + $dhcp_range ))
  30. if [ $b_range -gt $max_dhcp_range ]; then
  31. b_range=$max_dhcp_range
  32. fi
  33. a_range=$(( $b_range - $dhcp_range ))
  34. # Get the gateway
  35. gateway=$(ip route | grep default | awk '{print $3;}')
  36. # And the mac adress
  37. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  38. # Copy the config file
  39. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  40. # And set the config
  41. 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"
  42. 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"
  43. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  44. # Set a static ip for the server.
  45. echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf"
  46. fi
  47. exit 0