dnsmasq_regenconf_hook 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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# listen-address=" --target_file="${pending_conf}/etc/dnsmasq.conf"
  31. echo "
  32. conf-dir=/etc/dnsmasq.d/" >> "${pending_conf}/etc/dnsmasq.conf"
  33. #
  34. # Regen /etc/dnsmasq.d/02-pihole-dhcp.conf
  35. #
  36. enable_dhcp=$(ynh_app_setting_get --app=$app --key=enable_dhcp)
  37. if [ $enable_dhcp -eq 1 ]
  38. then
  39. # Get the default network interface
  40. # Find the IP associated to the network interface
  41. localipv4=$(ip address | grep "${main_iface}\$" | awk '{print $2;}' | cut -d/ -f1)
  42. max_dhcp_range=250
  43. dhcp_range=100
  44. # Define the dhcp range from the current ip
  45. ip_beginning_part=$(echo "$localipv4" | cut -d. -f1-3)
  46. ip_fourth_part=$(echo "$localipv4" | cut -d. -f4)
  47. b_range=$(( $ip_fourth_part + $dhcp_range ))
  48. if [ $b_range -gt $max_dhcp_range ]; then
  49. b_range=$max_dhcp_range
  50. fi
  51. a_range=$(( $b_range - $dhcp_range ))
  52. # Get the gateway
  53. gateway=$(ip route | grep default | awk '{print $3;}')
  54. # And the mac adress
  55. hw_adress=$(ip link | grep -A1 "$main_iface" | tail -n1 | awk '{print $2;}')
  56. # Copy the config file
  57. cp -a "/etc/yunohost/apps/$app/conf/02-pihole-dhcp.conf" "$dnsmasq_dir/"
  58. # And set the config
  59. 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"
  60. 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"
  61. ynh_replace_string --match_string="__GATEWAY__" --replace_string="$gateway" --target_file="${pending_conf}/etc/dnsmasq.d/02-pihole-dhcp.conf"
  62. # Set a static ip for the server.
  63. echo "dhcp-host=$hw_adress,$localipv4" > "${dnsmasq_dir}/04-pihole-static-dhcp.conf"
  64. fi
  65. exit 0