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.1 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 \
  20. 'No auth details provided. Please set user credentials using the \
  21. \$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. _debug2 response "$response"
  44. }
  45. #-- dns_he_rm() - Remove TXT record ------------------------------------
  46. # Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  47. dns_he_rm() {
  48. _full_domain=$1
  49. _txt_value=$2
  50. _info "Cleaning up after DNS-01 Hurricane Electric hook"
  51. # fills in the $_zone_id
  52. _find_zone $_full_domain || return 1
  53. _debug "Zone id \"$_zone_id\" will be used."
  54. # Find the record id to clean
  55. body="email=${HE_Username}&pass=${HE_Password}"
  56. body="$body&hosted_dns_zoneid=$_zone_id"
  57. body="$body&menu=edit_zone"
  58. body="$body&hosted_dns_editzone="
  59. _record_id=$(_post $body "https://dns.he.net/" \
  60. | tr -d '\n' \
  61. | _egrep_o "data=\"&quot;${_txt_value}&quot;([^>]+>){6}[^<]+<[^;]+;deleteRecord\('[0-9]+','${_full_domain}','TXT'\)" \
  62. | _egrep_o "[0-9]+','${_full_domain}','TXT'\)$" \
  63. | _egrep_o "^[0-9]+"
  64. )
  65. # The series of egreps above could have been done a bit shorter but
  66. # I wanted to double-check whether it's the correct record (in case
  67. # HE changes their website somehow).
  68. # Remove the record
  69. body="email=${HE_Username}&pass=${HE_Password}"
  70. body="$body&menu=edit_zone"
  71. body="$body&hosted_dns_zoneid=$_zone_id"
  72. body="$body&hosted_dns_recordid=$_record_id"
  73. body="$body&hosted_dns_editzone=1"
  74. body="$body&hosted_dns_delrecord=1"
  75. body="$body&hosted_dns_delconfirm=delete"
  76. body="$body&hosted_dns_editzone=1"
  77. _post $body "https://dns.he.net/" \
  78. | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
  79. > /dev/null
  80. if [ $? -eq 0 ]; then
  81. _info "Record removed successfuly."
  82. else
  83. _err \
  84. "Could not clean (remove) up the record. Please go to HE" \
  85. "administration interface and clean it by hand."
  86. fi
  87. }
  88. ########################## PRIVATE FUNCTIONS ###########################
  89. #-- _find_zone() -------------------------------------------------------
  90. # Returns the most specific zone found in administration interface.
  91. #
  92. # Example:
  93. #
  94. # _find_zone first.second.third.co.uk
  95. #
  96. # ... will return the first zone that exists in admin out of these:
  97. # - "first.second.third.co.uk"
  98. # - "second.third.co.uk"
  99. # - "third.co.uk"
  100. # - "co.uk" <-- unlikely
  101. # - "uk" <-'
  102. #
  103. # (another approach would be something like this:
  104. # https://github.com/hlandau/acme/blob/master/_doc/dns.hook
  105. # - that's better if there are multiple pages. It's so much simpler.
  106. # )
  107. _find_zone() {
  108. _domain="$1"
  109. ## _all_zones is an array that looks like this:
  110. ## ( zone1:id zone2:id ... )
  111. body="email=${HE_Username}&pass=${HE_Password}"
  112. _all_zones=( $(_post $body "https://dns.he.net/" \
  113. | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+" \
  114. | cut -d '"' -f 3,5 --output-delimiter=":" \
  115. ) )
  116. _strip_counter=1
  117. while true
  118. do
  119. _attempted_zone=$(echo $_domain | cut -d . -f ${_strip_counter}-)
  120. # All possible zone names have been tried
  121. if [ -z "$_attempted_zone" ]
  122. then
  123. _err "No zone for domain \"$_domain\" found."
  124. break
  125. fi
  126. # Walk through all zones on the account
  127. #echo "$_all_zones" | while IFS=' ' read _zone_name _zone_id
  128. for i in ${_all_zones[@]}
  129. do
  130. _zone_name=$(echo $i | cut -d ':' -f 1)
  131. _zone_id=$(echo $i | cut -d ':' -f 2)
  132. if [ "$_zone_name" = "$_attempted_zone" ]
  133. then
  134. # Zone found - we got $_zone_name and $_zone_id, let's get out...
  135. _debug "Found relevant zone \"$_zone_name\" with id" \
  136. "\"$_zone_id\" - will be used for domain \"$_domain\"."
  137. return 0
  138. fi
  139. done
  140. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try another \
  141. variation."
  142. _strip_counter=$(_math $_strip_counter + 1)
  143. done
  144. # No zone found.
  145. return 1
  146. }
  147. # vim: et:ts=2:sw=2: