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.

130 lines
3.6 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 component will copy root.key back to /etc/unbound/ periodically, but
  18. # avoid ROM flash abuse (UCI option).
  19. #
  20. ##############################################################################
  21. # while useful (sh)ellcheck is pedantic and noisy
  22. # shellcheck disable=1091,2002,2004,2034,2039,2086,2094,2140,2154,2155
  23. . /usr/lib/unbound/defaults.sh
  24. ##############################################################################
  25. roothints_update() {
  26. # TODO: Might not be implemented. Unbound doesn't natively update hints.
  27. # Unbound philosophy is built in root hints are good for machine life.
  28. return 0
  29. }
  30. ##############################################################################
  31. rootkey_update() {
  32. local basekey_date rootkey_date rootkey_age filestuff
  33. local dnssec=$( uci_get unbound.@unbound[0].validator )
  34. local dnssec_ntp=$( uci_get unbound.@unbound[0].validator_ntp )
  35. local dnssec_age=$( uci_get unbound.@unbound[0].root_age )
  36. # fix empty
  37. [ -z "$dnssec" ] && dnssec=0
  38. [ -z "$dnssec_ntp" ] && dnssec_ntp=1
  39. [ -z "$dnssec_age" ] && dnssec_age=9
  40. if [ $dnssec_age -gt 90 ] || [ $dnssec -lt 1 ] ; then
  41. # Feature disabled
  42. return 0
  43. elif [ "$dnssec_ntp" -gt 0 ] && [ ! -f "$UB_TIME_FILE" ] ; then
  44. # We don't have time yet
  45. return 0
  46. fi
  47. if [ -f /etc/unbound/root.key ] ; then
  48. basekey_date=$( date -r /etc/unbound/root.key +%s )
  49. else
  50. # No persistent storage key
  51. basekey_date=$( date -d 2000-01-01 +%s )
  52. fi
  53. if [ -f "$UB_RKEY_FILE" ] ; then
  54. # Unbound maintains it itself
  55. rootkey_date=$( date -r $UB_RKEY_FILE +%s )
  56. rootkey_age=$(( (rootkey_date - basekey_date) / 86440 ))
  57. elif [ -x "$UB_ANCHOR" ] ; then
  58. # No tmpfs key - use unbound-anchor
  59. rootkey_date=$( date -I +%s )
  60. rootkey_age=$(( (rootkey_date - basekey_date) / 86440 ))
  61. $UB_ANCHOR -a $UB_RKEY_FILE
  62. else
  63. # give up
  64. rootkey_age=0
  65. fi
  66. if [ $rootkey_age -gt $dnssec_age ] ; then
  67. filestuff=$( cat $UB_RKEY_FILE )
  68. case "$filestuff" in
  69. *NOERROR*)
  70. # Header comment for drill and dig
  71. logger -t unbound -s "root.key updated after $rootkey_age days"
  72. cp -p $UB_RKEY_FILE /etc/unbound/root.key
  73. ;;
  74. *"state=2 [ VALID ]"*)
  75. # Comment inline to key for unbound-anchor
  76. logger -t unbound -s "root.key updated after $rootkey_age days"
  77. cp -p $UB_RKEY_FILE /etc/unbound/root.key
  78. ;;
  79. *)
  80. logger -t unbound -s "root.key still $rootkey_age days old"
  81. ;;
  82. esac
  83. fi
  84. }
  85. ##############################################################################
  86. resolv_teardown() {
  87. case $( cat $UB_RESOLV_CONF ) in
  88. *"generated by Unbound UCI"*)
  89. # our resolver file, reset to auto resolver file.
  90. rm -f $UB_RESOLV_CONF
  91. ln -s $UB_RESOLV_AUTO $UB_RESOLV_CONF
  92. ;;
  93. esac
  94. }
  95. ##############################################################################
  96. unbound_stop() {
  97. resolv_teardown
  98. roothints_update
  99. rootkey_update
  100. }
  101. ##############################################################################