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.

175 lines
5.6 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&menu=edit_zone"
  32. body="$body&Type=TXT"
  33. body="$body&hosted_dns_zoneid=$_zone_id"
  34. body="$body&hosted_dns_recordid="
  35. body="$body&hosted_dns_editzone=1"
  36. body="$body&Priority="
  37. body="$body&Name=$_full_domain"
  38. body="$body&Content=$_txt_value"
  39. body="$body&TTL=300"
  40. body="$body&hosted_dns_editrecord=Submit"
  41. response="$(_post "$body" "https://dns.he.net/")"
  42. exit_code="$?"
  43. if [ "$exit_code" -eq 0 ]; then
  44. _info "TXT record added successfuly."
  45. else
  46. _err "Couldn't add the TXT record."
  47. fi
  48. _debug2 response "$response"
  49. return "$exit_code"
  50. }
  51. #-- dns_he_rm() - Remove TXT record ------------------------------------
  52. # Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  53. dns_he_rm() {
  54. _full_domain=$1
  55. _txt_value=$2
  56. _info "Cleaning up after DNS-01 Hurricane Electric hook"
  57. # fills in the $_zone_id
  58. _find_zone "$_full_domain" || return 1
  59. _debug "Zone id \"$_zone_id\" will be used."
  60. # Find the record id to clean
  61. body="email=${HE_Username}&pass=${HE_Password}"
  62. body="$body&hosted_dns_zoneid=$_zone_id"
  63. body="$body&menu=edit_zone"
  64. body="$body&hosted_dns_editzone="
  65. domain_regex="$(echo "$_full_domain" | sed 's/\./\\./g')" # escape dots
  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]+','${domain_regex}','TXT'\)" \
  69. | _egrep_o "[0-9]+','${domain_regex}','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. _post "$body" "https://dns.he.net/" \
  84. | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
  85. >/dev/null
  86. exit_code="$?"
  87. if [ "$exit_code" -eq 0 ]; then
  88. _info "Record removed successfuly."
  89. else
  90. _err "Could not clean (remove) up the record. Please go to HE administration interface and clean it by hand."
  91. return "$exit_code"
  92. fi
  93. }
  94. ########################## PRIVATE FUNCTIONS ###########################
  95. #-- _find_zone() -------------------------------------------------------
  96. # Returns the most specific zone found in administration interface.
  97. #
  98. # Example:
  99. #
  100. # _find_zone first.second.third.co.uk
  101. #
  102. # ... will return the first zone that exists in admin out of these:
  103. # - "first.second.third.co.uk"
  104. # - "second.third.co.uk"
  105. # - "third.co.uk"
  106. # - "co.uk" <-- unlikely
  107. # - "uk" <-'
  108. #
  109. # (another approach would be something like this:
  110. # https://github.com/hlandau/acme/blob/master/_doc/dns.hook
  111. # - that's better if there are multiple pages. It's so much simpler.
  112. # )
  113. _find_zone() {
  114. _domain="$1"
  115. body="email=${HE_Username}&pass=${HE_Password}"
  116. _matches=$(_post "$body" "https://dns.he.net/" \
  117. | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+"
  118. )
  119. # Zone names and zone IDs are in same order
  120. _zone_ids=$(echo "$_matches" | cut -d '"' -f 5)
  121. _zone_names=$(echo "$_matches" | cut -d '"' -f 3)
  122. _debug2 "These are the zones on this HE account:"
  123. _debug2 "$_zone_names"
  124. _debug2 "And these are their respective IDs:"
  125. _debug2 "$_zone_ids"
  126. # Walk through all possible zone names
  127. _strip_counter=1
  128. while true; do
  129. _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
  130. # All possible zone names have been tried
  131. if [ -z "$_attempted_zone" ]; then
  132. _err "No zone for domain \"$_domain\" found."
  133. return 1
  134. fi
  135. _debug "Looking for zone \"${_attempted_zone}\""
  136. # Take care of "." and only match whole lines. Note that grep -F
  137. # cannot be used because there's no way to make it match whole
  138. # lines.
  139. regex="^$(echo "$_attempted_zone" | sed 's/\./\\./g')$"
  140. line_num=$(echo "$_zone_names" \
  141. | grep -n "$regex" \
  142. | cut -d : -f 1
  143. )
  144. if [ -n "$line_num" ]; then
  145. _zone_id=$(echo "$_zone_ids" | sed "${line_num}q;d")
  146. _debug "Found relevant zone \"$_attempted_zone\" with id \"$_zone_id\" - will be used for domain \"$_domain\"."
  147. return 0
  148. fi
  149. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try a less specific zone."
  150. _strip_counter=$(_math "$_strip_counter" + 1)
  151. done
  152. }
  153. # vim: et:ts=2:sw=2: