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.

187 lines
5.8 KiB

  1. #!/usr/bin/env sh
  2. ########################################################################
  3. # Hurricane Electric hook script for acme.sh
  4. #
  5. # Environment variables:
  6. #
  7. # - $HE_Username (your dns.he.net username)
  8. # - $HE_Password (your dns.he.net password)
  9. #
  10. # Author: Ondrej Simek <me@ondrejsimek.com>
  11. # Git repo: https://github.com/angel333/acme.sh
  12. #-- dns_he_add() - Add TXT record --------------------------------------
  13. # Usage: dns_he_add _acme-challenge.subdomain.domain.com "XyZ123..."
  14. dns_he_add() {
  15. _full_domain=$1
  16. _txt_value=$2
  17. _info "Using DNS-01 Hurricane Electric hook"
  18. if [ -z "$HE_Username" ] || [ -z "$HE_Password" ]; then
  19. _err "No auth details provided. Please set user credentials using the \$HE_Username and \$HE_Password envoronment variables."
  20. return 1
  21. fi
  22. _saveaccountconf HE_Username "$HE_Username"
  23. _saveaccountconf HE_Password "$HE_Password"
  24. # fills in the $_zone_id
  25. _find_zone "$_full_domain" || return 1
  26. _debug "Zone id \"$_zone_id\" will be used."
  27. body="email=${HE_Username}&pass=${HE_Password}"
  28. body="$body&account="
  29. body="$body&account="
  30. body="$body&menu=edit_zone"
  31. body="$body&Type=TXT"
  32. body="$body&hosted_dns_zoneid=$_zone_id"
  33. body="$body&hosted_dns_recordid="
  34. body="$body&hosted_dns_editzone=1"
  35. body="$body&Priority="
  36. body="$body&Name=$_full_domain"
  37. body="$body&Content=$_txt_value"
  38. body="$body&TTL=300"
  39. body="$body&hosted_dns_editrecord=Submit"
  40. response="$(_post "$body" "https://dns.he.net/")"
  41. _debug2 response "$response"
  42. }
  43. #-- dns_he_rm() - Remove TXT record ------------------------------------
  44. # Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  45. dns_he_rm() {
  46. _full_domain=$1
  47. _txt_value=$2
  48. _info "Cleaning up after DNS-01 Hurricane Electric hook"
  49. # fills in the $_zone_id
  50. _find_zone "$_full_domain" || return 1
  51. _debug "Zone id \"$_zone_id\" will be used."
  52. # Find the record id to clean
  53. body="email=${HE_Username}&pass=${HE_Password}"
  54. body="$body&hosted_dns_zoneid=$_zone_id"
  55. body="$body&menu=edit_zone"
  56. body="$body&hosted_dns_editzone="
  57. _record_id=$(_post "$body" "https://dns.he.net/" \
  58. | tr -d '\n' \
  59. | _egrep_o "data=\"&quot;${_txt_value}&quot;([^>]+>){6}[^<]+<[^;]+;deleteRecord\('[0-9]+','${_full_domain}','TXT'\)" \
  60. | _egrep_o "[0-9]+','${_full_domain}','TXT'\)$" \
  61. | _egrep_o "^[0-9]+"
  62. )
  63. # The series of egreps above could have been done a bit shorter but
  64. # I wanted to double-check whether it's the correct record (in case
  65. # HE changes their website somehow).
  66. # Remove the record
  67. body="email=${HE_Username}&pass=${HE_Password}"
  68. body="$body&menu=edit_zone"
  69. body="$body&hosted_dns_zoneid=$_zone_id"
  70. body="$body&hosted_dns_recordid=$_record_id"
  71. body="$body&hosted_dns_editzone=1"
  72. body="$body&hosted_dns_delrecord=1"
  73. body="$body&hosted_dns_delconfirm=delete"
  74. body="$body&hosted_dns_editzone=1"
  75. _post "$body" "https://dns.he.net/" \
  76. | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
  77. >/dev/null
  78. if [ $? -eq 0 ]; then
  79. _info "Record removed successfuly."
  80. else
  81. _err \
  82. "Could not clean (remove) up the record. Please go to HE" \
  83. "administration interface and clean it by hand."
  84. fi
  85. }
  86. ########################## PRIVATE FUNCTIONS ###########################
  87. #-- _find_zone() -------------------------------------------------------
  88. # Returns the most specific zone found in administration interface.
  89. #
  90. # Example:
  91. #
  92. # _find_zone first.second.third.co.uk
  93. #
  94. # ... will return the first zone that exists in admin out of these:
  95. # - "first.second.third.co.uk"
  96. # - "second.third.co.uk"
  97. # - "third.co.uk"
  98. # - "co.uk" <-- unlikely
  99. # - "uk" <-'
  100. #
  101. # (another approach would be something like this:
  102. # https://github.com/hlandau/acme/blob/master/_doc/dns.hook
  103. # - that's better if there are multiple pages. It's so much simpler.
  104. # )
  105. _find_zone() {
  106. _domain="$1"
  107. body="email=${HE_Username}&pass=${HE_Password}"
  108. _matches=$(_post "$body" "https://dns.he.net/" \
  109. | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+"
  110. )
  111. # Zone names and zone IDs are in same order
  112. _zone_ids=$(echo "$_matches" | cut -d '"' -f 5 --output-delimiter=":")
  113. _zone_names=$(echo "$_matches" | cut -d '"' -f 3 --output-delimiter=":")
  114. _debug2 "These are the zones on this HE account:"
  115. _debug2 "$_zone_names"
  116. _debug2 "And these are their respective IDs:"
  117. _debug2 "$_zone_ids"
  118. # Walk through all possible zone names
  119. _strip_counter=1
  120. while true; do
  121. _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
  122. # All possible zone names have been tried
  123. if [ -z "$_attempted_zone" ]; then
  124. _err "No zone for domain \"$_domain\" found."
  125. return 1
  126. fi
  127. _debug "Looking for zone \"${_attempted_zone}\""
  128. _line_num=$(echo "$_zone_names" | _find_linenum "$_attempted_zone")
  129. if [ -n "$_line_num" ]; then
  130. _zone_id=$(echo "$_zone_ids" | sed "${_line_num}q;d")
  131. _debug "Found relevant zone \"$_attempted_zone\" with id" \
  132. "\"$_zone_id\" - will be used for domain \"$_domain\"."
  133. return 0
  134. fi
  135. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try a less" \
  136. "specific zone."
  137. _strip_counter=$(_math $_strip_counter + 1)
  138. done
  139. }
  140. #-- _find_linenum()-----------------------------------------------------
  141. # Returns line number of line (supplied as an argument) in STDIN.
  142. #
  143. # Example:
  144. #
  145. # printf "a\nb\nc" | _find_linenum "b"
  146. #
  147. # This will:
  148. # - print out 2 because that's the line number of "b"
  149. # - return code 0 because it was found
  150. _find_linenum() {
  151. _current_line_num=0
  152. while read line; do
  153. _current_line_num=$(expr "$_current_line_num" + 1)
  154. if [ "$line" = "$1" ]; then
  155. # Found! Let's echo the line number and quit
  156. echo $_current_line_num
  157. return 0
  158. fi
  159. done
  160. # Not found
  161. return 1
  162. }
  163. # vim: et:ts=2:sw=2: