odhcpd.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 longconf dateconf
  30. local dns_ls_add=$UNBOUND_VARDIR/dhcp_dns.add
  31. local dns_ls_del=$UNBOUND_VARDIR/dhcp_dns.del
  32. local dhcp_ls_new=$UNBOUND_VARDIR/dhcp_lease.new
  33. local dhcp_ls_old=$UNBOUND_VARDIR/dhcp_lease.old
  34. local dhcp_ls_add=$UNBOUND_VARDIR/dhcp_lease.add
  35. local dhcp_ls_del=$UNBOUND_VARDIR/dhcp_lease.del
  36. local dhcp_link=$( uci_get unbound.@unbound[0].dhcp_link )
  37. local dhcp4_slaac6=$( uci_get unbound.@unbound[0].dhcp4_slaac6 )
  38. local dhcp_domain=$( uci_get unbound.@unbound[0].domain )
  39. local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  40. if [ "$dhcp_link" = "odhcpd" -a -f "$dhcp_origin" ] ; then
  41. # Capture the lease file which could be changing often
  42. sort $dhcp_origin > $dhcp_ls_new
  43. if [ ! -f $UNBOUND_DHCP_CONF -o ! -f $dhcp_ls_old ] ; then
  44. longconf=2
  45. else
  46. dateconf=$(( $( date +%s ) - $( date -r $UNBOUND_DHCP_CONF +%s ) ))
  47. if [ $dateconf > 150 ] ; then
  48. longconf=1
  49. else
  50. longconf=0
  51. fi
  52. fi
  53. if [ $longconf -gt 0 ] ; then
  54. # Go through the messy business of coding up A, AAAA, and PTR records
  55. # This static conf will be available if Unbound restarts asynchronously
  56. awk -v hostfile=$UNBOUND_DHCP_CONF -v domain=$dhcp_domain \
  57. -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=1 \
  58. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  59. fi
  60. if [ $longconf -lt 2 ] ; then
  61. # Deleting and adding all records into Unbound can be a burden in a
  62. # high density environment. Use unbound-control incrementally.
  63. sort $dhcp_ls_old $dhcp_ls_new $dhcp_ls_new | uniq -u > $dhcp_ls_del
  64. awk -v hostfile=$dns_ls_del -v domain=$dhcp_domain \
  65. -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
  66. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_del
  67. sort $dhcp_ls_new $dhcp_ls_old $dhcp_ls_old | uniq -u > $dhcp_ls_add
  68. awk -v hostfile=$dns_ls_add -v domain=$dhcp_domain \
  69. -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
  70. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_add
  71. else
  72. awk -v hostfile=$dns_ls_add -v domain=$dhcp_domain \
  73. -v bslaac=$dhcp4_slaac6 -v bisolt=0 -v bconf=0 \
  74. -f /usr/lib/unbound/odhcpd.awk $dhcp_ls_new
  75. fi
  76. if [ -f "$dns_ls_del" ] ; then
  77. cat $dns_ls_del | $UNBOUND_CONTROL_CFG local_datas_remove
  78. fi
  79. if [ -f "$dns_ls_add" ] ; then
  80. cat $dns_ls_add | $UNBOUND_CONTROL_CFG local_datas
  81. fi
  82. # prepare next round
  83. mv $dhcp_ls_new $dhcp_ls_old
  84. rm -f $dns_ls_del $dns_ls_add $dhcp_ls_del $dhcp_ls_add
  85. fi
  86. }
  87. ##############################################################################
  88. odhcpd_zonedata
  89. ##############################################################################