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.

166 lines
5.1 KiB

8 years ago
  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. ## _all_zones is an array that looks like this:
  108. ## ( zone1:id zone2:id ... )
  109. body="email=${HE_Username}&pass=${HE_Password}"
  110. # TODO arrays aren't supported in POSIX sh
  111. _all_zones=($(_post "$body" "https://dns.he.net/" \
  112. | _egrep_o "delete_dom.*name=\"[^\"]+\" value=\"[0-9]+" \
  113. | cut -d '"' -f 3,5 --output-delimiter=":"
  114. ))
  115. _strip_counter=1
  116. while true; do
  117. _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
  118. # All possible zone names have been tried
  119. if [ -z "$_attempted_zone" ]; then
  120. _err "No zone for domain \"$_domain\" found."
  121. break
  122. fi
  123. # Walk through all zones on the account
  124. #echo "$_all_zones" | while IFS=' ' read _zone_name _zone_id
  125. for i in ${_all_zones[@]}; do
  126. _zone_name=$(echo "$i" | cut -d ':' -f 1)
  127. _zone_id=$(echo "$i" | cut -d ':' -f 2)
  128. if [ "$_zone_name" = "$_attempted_zone" ]; then
  129. # Zone found - we got $_zone_name and $_zone_id, let's get out...
  130. _debug "Found relevant zone \"$_zone_name\" with id" \
  131. "\"$_zone_id\" - will be used for domain \"$_domain\"."
  132. return 0
  133. fi
  134. done
  135. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try another \
  136. variation."
  137. _strip_counter=$(_math $_strip_counter + 1)
  138. done
  139. # No zone found.
  140. return 1
  141. }
  142. # vim: et:ts=2:sw=2: