dnsmasq_regenconf_hook 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # Regen /etc/dnsmasq.d/01-pihole.conf
  10. #
  11. dnsmasq_dir="${pending_conf}/etc/dnsmasq.d"
  12. mkdir -p "$dnsmasq_dir"
  13. main_iface=$(ip route | grep --max-count=1 default | awk '{print $5;}')
  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. ynh_replace_string --match_string="@CACHE_SIZE@" --replace_string="1000" --target_file="$dnsmasq_dir/01-pihole.conf"
  20. query_logging=$(ynh_app_setting_get --app=$app --key=query_logging)
  21. if [ "$query_logging" = "true" ]; then
  22. ynh_replace_string --match_string="^#log-queries" --replace_string="log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  23. else
  24. ynh_replace_string --match_string="^log-queries" --replace_string="#log-queries" --target_file="$dnsmasq_dir/01-pihole.conf"
  25. fi
  26. #
  27. # Tweak dnsmsasq's general conf cache-size
  28. #
  29. ynh_replace_string --match_string="^cache-size=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  30. ynh_replace_string --match_string="^listen-address=" --replace_string="#pihole# cache-size=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  31. echo "conf-dir=/etc/dnsmasq.d/" >> "${pending_conf}/etc/dnsmasq.conf"
  32. #
  33. # Regen /etc/dnsmasq.d/02-pihole-dhcp.conf
  34. #
  35. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  36. if [ $enable_dhcp -eq 1 ]
  37. then
  38. # Get the default network interface
  39. # Find the IP associated to the network interface
  40. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  41. max_dhcp_range=250
  42. dhcp_range=100
  43. # Define the dhcp range from the current ip
  44. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  45. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  46. b_range=$(( $ip_fourth_part + $dhcp_range ))
  47. if [ $b_range -gt $max_dhcp_range ]; then
  48. b_range=$max_dhcp_range
  49. fi
  50. a_range=$(( $b_range - $dhcp_range ))
  51. # Get the gateway
  52. gateway=$(ip route | grep default | awk '{print $3;}')
  53. # And the mac adress
  54. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  55. # Copy the config file
  56. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  57. # And set the config
  58. 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"
  59. 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"
  60. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  61. # Set a static ip for the server.
  62. echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf"
  63. fi
  64. exit 0