You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.2 KiB

  1. #!/bin/sh
  2. ##############################################################################
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # Copyright (C) 2016 Eric Luehrsen
  14. #
  15. ##############################################################################
  16. #
  17. # This crosses over to the dnsmasq UCI file "dhcp" and parses it for fields
  18. # that will allow Unbound to request local host DNS of dnsmasq. We need to look
  19. # at the interfaces in "dhcp" and get their subnets. The Unbound conf syntax
  20. # makes this a little difficult. First in "server:" we need to create private
  21. # zones for the domain and PTR records. Then we need to create numerous
  22. # "forward:" clauses to forward those zones to dnsmasq.
  23. #
  24. ##############################################################################
  25. create_local_zone() {
  26. local cfg="$1"
  27. local fwd_port fwd_domain
  28. config_get fwd_domain "$cfg" domain
  29. config_get fwd_port "$cfg" port
  30. if [ -n "$fwd_domain" -a -n "$fwd_port" -a ! "$fwd_port" -eq 53 ] ; then
  31. # dnsmasq localhost listening ports (possible multiple instances)
  32. UNBOUND_N_FWD_PORTS="$UNBOUND_N_FWD_PORTS $fwd_port"
  33. UNBOUND_TXT_FWD_ZONE="$UNBOUND_TXT_FWD_ZONE $fwd_domain"
  34. {
  35. # This creates DOMAIN local privledges
  36. echo " private-domain: \"$fwd_domain\""
  37. echo " local-zone: \"$fwd_domain.\" transparent"
  38. echo " domain-insecure: \"$fwd_domain\""
  39. echo
  40. } >> $UNBOUND_CONFFILE
  41. fi
  42. }
  43. ##############################################################################
  44. create_local_arpa() {
  45. local cfg="$1"
  46. local logint dhcpv4 dhcpv6 ignore
  47. local subnets subnets4 subnets6
  48. local forward arpa
  49. local validip4 validip6 privateip
  50. config_get logint "$cfg" interface
  51. config_get dhcpv4 "$cfg" dhcpv4
  52. config_get dhcpv6 "$cfg" dhcpv6
  53. config_get_bool ignore "$cfg" ignore 0
  54. # Find the list of addresses assigned to a logical interface
  55. # Its typical to have a logical gateway split NAME and NAME6
  56. network_get_subnets subnets4 "$logint"
  57. network_get_subnets6 subnets6 "$logint"
  58. subnets="$subnets4 $subnets6"
  59. network_get_subnets subnets4 "${logint}6"
  60. network_get_subnets6 subnets6 "${logint}6"
  61. subnets="$subnets $subnets4 $subnets6"
  62. if [ -z "$subnets" ] ; then
  63. forward=""
  64. elif [ -z "$UNBOUND_N_FWD_PORTS" ] ; then
  65. forward=""
  66. elif [ "$ignore" -gt 0 ] ; then
  67. if [ "$UNBOUND_B_GATE_NAME" -gt 0 ] ; then
  68. # Only forward the one gateway host.
  69. forward="host"
  70. else
  71. forward=""
  72. fi
  73. else
  74. # Forward the entire private subnet.
  75. forward="domain"
  76. fi
  77. if [ -n "$forward" ] ; then
  78. for subnet in $subnets ; do
  79. validip4=$( valid_subnet4 $subnet )
  80. validip6=$( valid_subnet6 $subnet )
  81. privateip=$( private_subnet $subnet )
  82. if [ "$validip4" = "ok" -a "$dhcpv4" != "disable" ] ; then
  83. if [ "$forward" = "domain" ] ; then
  84. arpa=$( domain_ptr_ip4 "$subnet" )
  85. else
  86. arpa=$( host_ptr_ip4 "$subnet" )
  87. fi
  88. elif [ "$validip6" = "ok" -a "$dhcpv6" != "disable" ] ; then
  89. if [ "$forward" = "domain" ] ; then
  90. arpa=$( domain_ptr_ip6 "$subnet" )
  91. else
  92. arpa=$( host_ptr_ip6 "$subnet" )
  93. fi
  94. else
  95. arpa=""
  96. fi
  97. if [ -n "$arpa" ] ; then
  98. if [ "$privateip" = "ok" ] ; then
  99. {
  100. # This creates ARPA local zone privledges
  101. echo " local-zone: \"$arpa.\" transparent"
  102. echo " domain-insecure: \"$arpa\""
  103. echo
  104. } >> $UNBOUND_CONFFILE
  105. fi
  106. UNBOUND_TXT_FWD_ZONE="$UNBOUND_TXT_FWD_ZONE $arpa"
  107. fi
  108. done
  109. fi
  110. }
  111. ##############################################################################
  112. forward_local_zone() {
  113. if [ -n "$UNBOUND_N_FWD_PORTS" -a -n "$UNBOUND_TXT_FWD_ZONE" ] ; then
  114. for fwd_domain in $UNBOUND_TXT_FWD_ZONE ; do
  115. {
  116. # This is derived of create_local_zone/arpa
  117. # but forward: clauses need to be seperate
  118. echo "forward-zone:"
  119. echo " name: \"$fwd_domain.\""
  120. for port in $UNBOUND_N_FWD_PORTS ; do
  121. echo " forward-addr: 127.0.0.1@$port"
  122. done
  123. echo
  124. } >> $UNBOUND_CONFFILE
  125. done
  126. fi
  127. }
  128. ##############################################################################
  129. dnsmasq_link() {
  130. ####################
  131. # UCI @ dhcp #
  132. ####################
  133. if [ "$UNBOUND_B_DNSMASQ" -gt 0 ] ; then
  134. # Forward to dnsmasq on same host for DHCP lease hosts
  135. echo " do-not-query-localhost: no" >> $UNBOUND_CONFFILE
  136. # Look at dnsmasq settings
  137. config_load dhcp
  138. # Zone for DHCP / SLAAC-PING DOMAIN
  139. config_foreach create_local_zone dnsmasq
  140. # Zone for DHCP / SLAAC-PING ARPA
  141. config_foreach create_local_arpa dhcp
  142. # Now create ALL seperate forward: clauses
  143. forward_local_zone
  144. fi
  145. }
  146. ##############################################################################