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.

200 lines
5.6 KiB

  1. #!/usr/bin/env sh
  2. # TODO Somehow use _get instead of curl - not sure how to support
  3. # cookies though...
  4. ########################################################################
  5. # Hurricane Electric hook script for acme.sh
  6. #
  7. # Environment variables:
  8. #
  9. # - $HE_Username (your dns.he.net username)
  10. # - $HE_Password (your dns.he.net password)
  11. #
  12. # Author: Ondrej Simek <me@ondrejsimek.com>
  13. # Git repo: https://github.com/angel333/acme.sh
  14. #-- dns_he_add() - Add TXT record --------------------------------------
  15. # Usage: dns_he_add _acme-challenge.subdomain.domain.com "XyZ123..."
  16. dns_he_add() {
  17. _full_domain=$1
  18. _txt_value=$2
  19. _info "Using DNS-01 Hurricane Electric hook"
  20. _authenticate || return 1
  21. _saveaccountconf HE_Username "$HE_Username"
  22. _saveaccountconf HE_Password "$HE_Password"
  23. # fills in the $_zone_id
  24. _find_zone $_full_domain || return 1
  25. _debug "Zone id \"$_zone_id\" will be used."
  26. curl -L --silent --show-error --cookie "$_he_cookie" \
  27. --form "account=" \
  28. --form "menu=edit_zone" \
  29. --form "Type=TXT" \
  30. --form "hosted_dns_zoneid=$_zone_id" \
  31. --form "hosted_dns_recordid=" \
  32. --form "hosted_dns_editzone=1" \
  33. --form "Priority=" \
  34. --form "Name=$_full_domain" \
  35. --form "Content=$_txt_value" \
  36. --form "TTL=300" \
  37. --form "hosted_dns_editrecord=Submit" \
  38. "https://dns.he.net/" \
  39. > /dev/null
  40. }
  41. #-- dns_he_rm() - Remove TXT record ------------------------------------
  42. # Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  43. dns_he_rm() {
  44. _full_domain=$1
  45. _txt_value=$2
  46. _info "Cleaning up after DNS-01 Hurricane Electric hook"
  47. _authenticate || return 1
  48. # fills in the $_zone_id
  49. _find_zone $_full_domain || return 1
  50. _debug "Zone id \"$_zone_id\" will be used."
  51. # Find the record id to clean
  52. _record_id=$( \
  53. curl -L --silent --show-error --cookie "$_he_cookie" \
  54. "https://dns.he.net/?hosted_dns_zoneid=$_zone_id&menu=edit_zone&hosted_dns_editzone" \
  55. | grep -A 1 "data=\"\(&quot;\)\?${_txt_value}\(&quot;\)\?\"" \
  56. | tail -n 1 \
  57. | _egrep_o "'[[:digit:]]+','[^']+','TXT'" \
  58. | cut -b 2- \
  59. | _egrep_o "[[:digit:]]+" \
  60. | head -n1) # ... oh my, what have I done...
  61. # Remove the record
  62. curl -L --silent --show-error --cookie "$_he_cookie" \
  63. --form "menu=edit_zone" \
  64. --form "hosted_dns_zoneid=$_zone_id" \
  65. --form "hosted_dns_recordid=$_record_id" \
  66. --form "hosted_dns_editzone=1" \
  67. --form "hosted_dns_delrecord=1" \
  68. --form "hosted_dns_delconfirm=delete" \
  69. --form "hosted_dns_editzone=1" \
  70. "https://dns.he.net/" \
  71. | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
  72. > /dev/null
  73. if [ $? -eq 0 ]; then
  74. _info "Record removed successfuly."
  75. else
  76. _err \
  77. "Could not clean (remove) up the record. Please go to HE" \
  78. "administration interface and clean it by hand."
  79. fi
  80. }
  81. ########################## PRIVATE FUNCTIONS ###########################
  82. #-- _find_zone() -------------------------------------------------------
  83. # Usage: _authenticate
  84. #
  85. # - needs $HE_Username and $HE_Password
  86. # - sets the $_he_cookie
  87. _authenticate() {
  88. if [ -z "$HE_Username" ] && [ -z "$HE_Password" ]; then
  89. _err \
  90. 'No auth details provided. Please set user credentials using the \
  91. \$HE_Username and \$HE_Password envoronment variables.'
  92. return 1
  93. fi
  94. # Just get a session
  95. _he_cookie=$( \
  96. curl -L --silent --show-error -I "https://dns.he.net/" \
  97. | grep '^Set-Cookie:' \
  98. | _egrep_o 'CGISESSID=[a-z0-9]*')
  99. # Attempt login
  100. curl -L --silent --show-error --cookie "$_he_cookie" \
  101. --form "email=${HE_Username}" \
  102. --form "pass=${HE_Password}" \
  103. "https://dns.he.net/" \
  104. > /dev/null
  105. # TODO detect unsuccessful logins
  106. }
  107. #-- _find_zone() -------------------------------------------------------
  108. # Returns the most specific zone found in administration interface.
  109. #
  110. # - needs $_he_cookie
  111. #
  112. # Example:
  113. #
  114. # _find_zone first.second.third.co.uk
  115. #
  116. # ... will return the first zone that exists in admin out of these:
  117. # - "first.second.third.co.uk"
  118. # - "second.third.co.uk"
  119. # - "third.co.uk"
  120. # - "co.uk" <-- unlikely
  121. # - "uk" <-'
  122. #
  123. # (another approach would be something like this:
  124. # https://github.com/hlandau/acme/blob/master/_doc/dns.hook
  125. # - that's better if there are multiple pages. It's so much simpler.
  126. # )
  127. _find_zone() {
  128. _domain="$1"
  129. ## _all_zones is an array that looks like this:
  130. ## ( zone1:id zone2:id ... )
  131. _all_zones=( $(curl -L --silent --show-error --cookie "$_he_cookie" \
  132. "https://dns.he.net/" \
  133. | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+" \
  134. | cut -d '"' -f 3,5 --output-delimiter=":" \
  135. ) )
  136. _strip_counter=1
  137. while [ true ]
  138. do
  139. _attempted_zone=$(echo $_domain | cut -d . -f ${_strip_counter}-)
  140. # All possible zone names have been tried
  141. if [ "$_attempted_zone" == "" ]
  142. then
  143. _err "No zone for domain \"$_domain\" found."
  144. break
  145. fi
  146. # Walk through all zones on the account
  147. #echo "$_all_zones" | while IFS=' ' read _zone_name _zone_id
  148. for i in ${_all_zones[@]}
  149. do
  150. _zone_name=$(echo $i | cut -d ':' -f 1)
  151. _zone_id=$(echo $i | cut -d ':' -f 2)
  152. if [ "$_zone_name" == "$_attempted_zone" ]
  153. then
  154. # Zone found - we got $_zone_name and $_zone_id, let's get out...
  155. _debug "Found relevant zone \"$_zone_name\" with id" \
  156. "\"$_zone_id\" - will be used for domain \"$_domain\"."
  157. return 0
  158. fi
  159. done
  160. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try another \
  161. variation."
  162. _strip_counter=$(expr $_strip_counter + 1)
  163. done
  164. # No zone found.
  165. return 1
  166. }
  167. # vim: et:ts=2:sw=2: