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.

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