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.

124 lines
3.9 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 script facilitates alternate installation of Unbound+odhcpd and no
  18. # need for dnsmasq. There are some limitations, but it works and is small.
  19. # The lease file is parsed to make "zone-data:" and "local-data:" entries.
  20. #
  21. # config odhcpd 'odhcpd'
  22. # option leasetrigger '/usr/lib/unbound/odhcpd.sh'
  23. #
  24. ##############################################################################
  25. . /lib/functions.sh
  26. . /usr/lib/unbound/defaults.sh
  27. ##############################################################################
  28. odhcpd_zonedata() {
  29. local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link )
  30. local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 )
  31. local dhcp_domain=$( uci_get unbound.@unbound[0].domain )
  32. local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  33. if [ -f "$UB_TOTAL_CONF" ] && [ -f "$dhcp_origin" ] \
  34. && [ "$dhcp_link" = "odhcpd" ] && [ -n "$dhcp_domain" ] ; then
  35. local longconf dateconf
  36. local dns_ls_add=$UB_VARDIR/dhcp_dns.add
  37. local dns_ls_del=$UB_VARDIR/dhcp_dns.del
  38. local dns_ls_new=$UB_VARDIR/dhcp_dns.new
  39. local dns_ls_old=$UB_VARDIR/dhcp_dns.old
  40. local dhcp_ls_new=$UB_VARDIR/dhcp_lease.new
  41. # Capture the lease file which could be changing often
  42. sort $dhcp_origin > $dhcp_ls_new
  43. if [ ! -f $UB_DHCP_CONF ] || [ ! -f $dns_ls_old ] ; then
  44. # no old files laying around
  45. longconf=freshstart
  46. else
  47. # incremental at high load or full refresh about each 5 minutes
  48. dateconf=$(( $( date +%s ) - $( date -r $UB_DHCP_CONF +%s ) ))
  49. if [ $dateconf -gt 300 ] ; then
  50. longconf=longtime
  51. else
  52. longconf=increment
  53. fi
  54. fi
  55. case $longconf in
  56. freshstart)
  57. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  58. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  59. -v bisolt=0 -v bconf=1 \
  60. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  61. cp $dns_ls_new $dns_ls_add
  62. cp $dns_ls_new $dns_ls_old
  63. ;;
  64. longtime)
  65. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  66. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  67. -v bisolt=0 -v bconf=1 \
  68. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  69. awk '{ print $1 }' $dns_ls_old | sort | uniq > $dns_ls_del
  70. cp $dns_ls_new $dns_ls_add
  71. cp $dns_ls_new $dns_ls_old
  72. ;;
  73. *)
  74. # incremental add and prepare the old list for delete later
  75. # unbound-control can be slow so high DHCP rates cannot run a full list
  76. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  77. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  78. -v bisolt=0 -v bconf=0 \
  79. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  80. sort $dns_ls_new $dns_ls_old $dns_ls_old | uniq -u > $dns_ls_add
  81. sort $dns_ls_new $dns_ls_old | uniq > $dns_ls_old
  82. ;;
  83. esac
  84. if [ -f "$dns_ls_del" ] ; then
  85. cat $dns_ls_del | $UB_CONTROL_CFG local_datas_remove
  86. fi
  87. if [ -f "$dns_ls_add" ] ; then
  88. cat $dns_ls_add | $UB_CONTROL_CFG local_datas
  89. fi
  90. # prepare next round
  91. rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new
  92. fi
  93. }
  94. ##############################################################################
  95. odhcpd_zonedata
  96. ##############################################################################