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.

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