dnssec-signer 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env bash
  2. #####################################################
  3. # This script require the following
  4. #
  5. # * A path structure like this:
  6. # [..path..]/<domainname.tld>/
  7. # * This path contains a zone file named `db`
  8. #
  9. # Your named.conf.local file must link to a
  10. # filed named `db.signed`, e.g
  11. #
  12. # zone "domain.tld" {
  13. # type master;
  14. # file "[..path..]/domain.tld/db.signed";
  15. # };
  16. #
  17. # USAGE:
  18. #
  19. # 1. Make `dnssec-signer` executable:
  20. # chmod +x /path/to/dnssec-signer
  21. #
  22. # 2. Run:
  23. # ./path/to/dnssec-signer <domain.tld>
  24. # 3. Or, for every domain in path
  25. # ./path/to/dnssec-signer
  26. #
  27. ######################################################
  28. ######################################################
  29. # START OF CONFIG #
  30. ######################################################
  31. # SET YOU SYSTEM SPECIFIC DATA (which systemctl e.g) #
  32. ZONESDIR="/etc/bind/zones"
  33. CHECKZONE=/usr/sbin/named-checkzone
  34. CHECKCONF=/usr/sbin/named-checkconf
  35. KEYGEN=/usr/sbin/dnssec-keygen
  36. SIGNZONE=/usr/sbin/dnssec-signzone
  37. SYSCTL=/bin/systemctl
  38. DNSSERVICE="bind9.service"
  39. ######################################################
  40. # END OF CONFIG #
  41. ######################################################
  42. # Collect arguments
  43. ZONES="${*}"
  44. IFS=' ', read -r -a ZONES <<< "${ZONES}"
  45. # Generating keys for zone
  46. # Note, this doesnt check if they exists!
  47. function genkeys {
  48. ZONE=${1}
  49. echo "* Generating missing keys for ZONE ${ZONE}"
  50. ${KEYGEN} -a NSEC3RSASHA1 -b 2048 -n ZONE "${ZONE}"
  51. if [ $? -ne 0 ]; then
  52. return 1
  53. fi
  54. ${KEYGEN} -f KSK -a NSEC3RSASHA1 -b 4096 -n ZONE "${ZONE}"
  55. if [ $? -ne 0 ]; then
  56. return 1
  57. fi
  58. return 0
  59. }
  60. # Sign zone
  61. # The actual signing process.
  62. function sign {
  63. ZONE="${1}"
  64. echo " * Signing ZONE ${ZONE}"
  65. if ! [ -d "${2}" ]; then
  66. echo "! Error, path ${2} doesnt exist, abort."
  67. return 1
  68. fi
  69. cd "${2}"
  70. echo `pwd`
  71. F="db"
  72. if ! [ -w "${F}" ]; then
  73. echo "! Missing $(pwd)/${F}, abort."
  74. return 1
  75. fi
  76. ZONEF="db.zone"
  77. ZONEINCF="${ZONEF}.include"
  78. KEYS=()
  79. for KEY in $(ls "./"); do
  80. if [[ "${KEY}" == "K${ZONE}"* ]] && [[ "${KEY}" == *key ]]; then
  81. KEYS[${#KEYS[@]}]="${KEY}"
  82. fi
  83. done
  84. if [ "${#KEYS[@]}" -ne 2 ]; then
  85. if [ "${#KEYS[@]}" -ne 0 ]; then
  86. rm "${KEYS[@]}"
  87. fi
  88. genkeys "${ZONE}"
  89. if [ $? -ne 0 ]; then
  90. echo "! Error generating keys, abort."
  91. return 1
  92. fi
  93. sign "${ZONE}"
  94. return $?
  95. fi
  96. cat "${F}" > "${ZONEINCF}"
  97. for KEY in "${KEYS[@]}"; do
  98. echo "\$INCLUDE ${KEY}" >> "${ZONEINCF}"
  99. done
  100. SALT=$(head -c 1000 /dev/random | sha1sum | cut -b 1-16)
  101. ${SIGNZONE} -A -3 "${SALT}" -N increment -f "${ZONEF}" -o "${ZONE}" -t "${ZONEINCF}"
  102. if [ $? -ne 0 ]; then
  103. echo "! Error signing zone ${ZONE}, abort."
  104. return 2
  105. fi
  106. echo "* Checking configuration: "
  107. ${CHECKZONE} "${ZONE}" "${ZONEF}"
  108. if [ $? -ne 0 ]; then
  109. echo "! Error in configuration for ${ZONE} => ${SONEF}."
  110. return 2
  111. fi
  112. return 0
  113. }
  114. # Sign zone;
  115. # Checks that dir exists first...
  116. # and ensures we're entering and leaving correctly
  117. function signzone {
  118. SIGN="${1}"
  119. CWDIR=$(pwd)
  120. sign "${1}" "${ZONESDIR}/${SIGN}"
  121. RET=$?
  122. cd "${CWDIR}"
  123. return ${RET}
  124. }
  125. ERR=0
  126. for ZONE in "${ZONES[@]}"; do
  127. signzone "${ZONE}"
  128. if [ $? -eq 2 ]; then
  129. # To prevent restarting dns if failure
  130. ERR=1
  131. fi
  132. done;
  133. if [ ${ERR} -eq 0 ]; then
  134. ${CHECKCONF}
  135. if [ $? -ne 0 ]; then
  136. echo "! Error in configruation, not reloading Bind"
  137. else
  138. echo "* Restarting ${DNSSERVICE}"
  139. ${SYSCTL} restart "${DNSSERVICE}"
  140. fi
  141. else
  142. echo "Errors in some configurations, not restarting bind."
  143. fi