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.

150 lines
4.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. # while useful (sh)ellcheck is pedantic and noisy
  26. # shellcheck disable=1091,2002,2004,2034,2039,2086,2094,2140,2154,2155
  27. UB_ODHCPD_BLANK=
  28. ##############################################################################
  29. odhcpd_zonedata() {
  30. . /lib/functions.sh
  31. . /usr/lib/unbound/defaults.sh
  32. local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link )
  33. local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 )
  34. local dhcp_domain=$( uci_get unbound.@unbound[0].domain )
  35. local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  36. if [ -f "$UB_TOTAL_CONF" ] && [ -f "$dhcp_origin" ] \
  37. && [ "$dhcp_link" = "odhcpd" ] && [ -n "$dhcp_domain" ] ; then
  38. local longconf dateconf dateoldf
  39. local dns_ls_add=$UB_VARDIR/dhcp_dns.add
  40. local dns_ls_del=$UB_VARDIR/dhcp_dns.del
  41. local dns_ls_new=$UB_VARDIR/dhcp_dns.new
  42. local dns_ls_old=$UB_VARDIR/dhcp_dns.old
  43. local dhcp_ls_new=$UB_VARDIR/dhcp_lease.new
  44. if [ ! -f $UB_DHCP_CONF ] || [ ! -f $dns_ls_old ] ; then
  45. # no old files laying around
  46. touch $dns_ls_old
  47. sort $dhcp_origin > $dhcp_ls_new
  48. longconf=freshstart
  49. else
  50. # incremental at high load or full refresh about each 5 minutes
  51. dateconf=$(( $( date +%s ) - $( date -r $UB_DHCP_CONF +%s ) ))
  52. dateoldf=$(( $( date +%s ) - $( date -r $dns_ls_old +%s ) ))
  53. if [ $dateconf -gt 300 ] ; then
  54. touch $dns_ls_old
  55. sort $dhcp_origin > $dhcp_ls_new
  56. longconf=longtime
  57. elif [ $dateoldf -gt 1 ] ; then
  58. touch $dns_ls_old
  59. sort $dhcp_origin > $dhcp_ls_new
  60. longconf=increment
  61. else
  62. # odhcpd is rapidly updating leases a race condition could occur
  63. longconf=skip
  64. fi
  65. fi
  66. case $longconf in
  67. freshstart)
  68. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  69. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  70. -v bisolt=0 -v bconf=1 \
  71. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  72. cp $dns_ls_new $dns_ls_add
  73. cp $dns_ls_new $dns_ls_old
  74. cat $dns_ls_add | $UB_CONTROL_CFG local_datas
  75. rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new
  76. ;;
  77. longtime)
  78. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  79. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  80. -v bisolt=0 -v bconf=1 \
  81. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  82. awk '{ print $1 }' $dns_ls_old | sort | uniq > $dns_ls_del
  83. cp $dns_ls_new $dns_ls_add
  84. cp $dns_ls_new $dns_ls_old
  85. cat $dns_ls_del | $UB_CONTROL_CFG local_datas_remove
  86. cat $dns_ls_add | $UB_CONTROL_CFG local_datas
  87. rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new
  88. ;;
  89. increment)
  90. # incremental add and prepare the old list for delete later
  91. # unbound-control can be slow so high DHCP rates cannot run a full list
  92. awk -v conffile=$UB_DHCP_CONF -v pipefile=$dns_ls_new \
  93. -v domain=$dhcp_domain -v bslaac=$dhcp4_slaac6 \
  94. -v bisolt=0 -v bconf=0 \
  95. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  96. sort $dns_ls_new $dns_ls_old $dns_ls_old | uniq -u > $dns_ls_add
  97. sort $dns_ls_new $dns_ls_old | uniq > $dns_ls_old
  98. cat $dns_ls_add | $UB_CONTROL_CFG local_datas
  99. rm -f $dns_ls_new $dns_ls_del $dns_ls_add $dhcp_ls_new
  100. ;;
  101. *)
  102. echo "do nothing" >/dev/null
  103. ;;
  104. esac
  105. fi
  106. }
  107. ##############################################################################
  108. UB_ODHPCD_LOCK=/tmp/unbound_odhcpd.lock
  109. if [ ! -f $UB_ODHPCD_LOCK ] ; then
  110. # imperfect but it should avoid collisions
  111. touch $UB_ODHPCD_LOCK
  112. odhcpd_zonedata
  113. rm -f $UB_ODHPCD_LOCK
  114. else
  115. UB_ODHCPD_LOCK_AGE=$(( $( date +%s ) - $( date -r $UB_ODHPCD_LOCK +%s ) ))
  116. if [ $UB_ODHCPD_LOCK_AGE -gt 100 ] ; then
  117. # unlock because something likely broke but do not write this time through
  118. rm -f $UB_ODHPCD_LOCK
  119. fi
  120. fi
  121. ##############################################################################