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.

158 lines
5.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 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. HE_Username="${HE_Username:-$(_readaccountconf_mutable HE_Username)}"
  19. HE_Password="${HE_Password:-$(_readaccountconf_mutable HE_Password)}"
  20. if [ -z "$HE_Username" ] || [ -z "$HE_Password" ]; then
  21. HE_Username=
  22. HE_Password=
  23. _err "No auth details provided. Please set user credentials using the \$HE_Username and \$HE_Password envoronment variables."
  24. return 1
  25. fi
  26. _saveaccountconf_mutable HE_Username "$HE_Username"
  27. _saveaccountconf_mutable HE_Password "$HE_Password"
  28. # Fills in the $_zone_id
  29. _find_zone "$_full_domain" || return 1
  30. _debug "Zone id \"$_zone_id\" will be used."
  31. body="email=${HE_Username}&pass=${HE_Password}"
  32. body="$body&account="
  33. body="$body&menu=edit_zone"
  34. body="$body&Type=TXT"
  35. body="$body&hosted_dns_zoneid=$_zone_id"
  36. body="$body&hosted_dns_recordid="
  37. body="$body&hosted_dns_editzone=1"
  38. body="$body&Priority="
  39. body="$body&Name=$_full_domain"
  40. body="$body&Content=$_txt_value"
  41. body="$body&TTL=300"
  42. body="$body&hosted_dns_editrecord=Submit"
  43. response="$(_post "$body" "https://dns.he.net/")"
  44. exit_code="$?"
  45. if [ "$exit_code" -eq 0 ]; then
  46. _info "TXT record added successfully."
  47. else
  48. _err "Couldn't add the TXT record."
  49. fi
  50. _debug2 response "$response"
  51. return "$exit_code"
  52. }
  53. #-- dns_he_rm() - Remove TXT record ------------------------------------
  54. # Usage: dns_he_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  55. dns_he_rm() {
  56. _full_domain=$1
  57. _txt_value=$2
  58. _info "Cleaning up after DNS-01 Hurricane Electric hook"
  59. HE_Username="${HE_Username:-$(_readaccountconf_mutable HE_Username)}"
  60. HE_Password="${HE_Password:-$(_readaccountconf_mutable HE_Password)}"
  61. # fills in the $_zone_id
  62. _find_zone "$_full_domain" || return 1
  63. _debug "Zone id \"$_zone_id\" will be used."
  64. # Find the record id to clean
  65. body="email=${HE_Username}&pass=${HE_Password}"
  66. body="$body&hosted_dns_zoneid=$_zone_id"
  67. body="$body&menu=edit_zone"
  68. body="$body&hosted_dns_editzone="
  69. response="$(_post "$body" "https://dns.he.net/")"
  70. _debug2 "response" "$response"
  71. if ! _contains "$response" "$_txt_value"; then
  72. _debug "The txt record is not found, just skip"
  73. return 0
  74. fi
  75. _record_id="$(echo "$response" | tr -d "#" | sed "s/<tr/#<tr/g" | tr -d "\n" | tr "#" "\n" | grep "$_full_domain" | grep '"dns_tr"' | grep "$_txt_value" | cut -d '"' -f 4)"
  76. _debug2 _record_id "$_record_id"
  77. if [ -z "$_record_id" ]; then
  78. _err "Can not find record id"
  79. return 1
  80. fi
  81. # Remove the record
  82. body="email=${HE_Username}&pass=${HE_Password}"
  83. body="$body&menu=edit_zone"
  84. body="$body&hosted_dns_zoneid=$_zone_id"
  85. body="$body&hosted_dns_recordid=$_record_id"
  86. body="$body&hosted_dns_editzone=1"
  87. body="$body&hosted_dns_delrecord=1"
  88. body="$body&hosted_dns_delconfirm=delete"
  89. _post "$body" "https://dns.he.net/" \
  90. | grep '<div id="dns_status" onClick="hideThis(this);">Successfully removed record.</div>' \
  91. >/dev/null
  92. exit_code="$?"
  93. if [ "$exit_code" -eq 0 ]; then
  94. _info "Record removed successfully."
  95. else
  96. _err "Could not clean (remove) up the record. Please go to HE administration interface and clean it by hand."
  97. return "$exit_code"
  98. fi
  99. }
  100. ########################## PRIVATE FUNCTIONS ###########################
  101. _find_zone() {
  102. _domain="$1"
  103. body="email=${HE_Username}&pass=${HE_Password}"
  104. response="$(_post "$body" "https://dns.he.net/")"
  105. _debug2 response "$response"
  106. _table="$(echo "$response" | tr -d "#" | sed "s/<table/#<table/g" | tr -d "\n" | tr "#" "\n" | grep 'id="domains_table"')"
  107. _debug2 _table "$_table"
  108. _matches="$(echo "$_table" | sed "s/<tr/#<tr/g" | tr "#" "\n" | grep 'alt="edit"' | tr -d " " | sed "s/<td/#<td/g" | tr "#" "\n" | sed -n 3p)"
  109. _debug2 _matches "$_matches"
  110. # Zone names and zone IDs are in same order
  111. _zone_ids=$(echo "$_matches" | _egrep_o "hosted_dns_zoneid=[0-9]*&" | cut -d = -f 2 | tr -d '&')
  112. _zone_names=$(echo "$_matches" | _egrep_o "name=.*onclick" | cut -d '"' -f 2)
  113. _debug2 "These are the zones on this HE account:"
  114. _debug2 "$_zone_names"
  115. _debug2 "And these are their respective IDs:"
  116. _debug2 "$_zone_ids"
  117. if [ -z "$_zone_names" ] || [ -z "$_zone_ids" ]; then
  118. _err "Can not get zone names."
  119. return 1
  120. fi
  121. # Walk through all possible zone names
  122. _strip_counter=1
  123. while true; do
  124. _attempted_zone=$(echo "$_domain" | cut -d . -f ${_strip_counter}-)
  125. # All possible zone names have been tried
  126. if [ -z "$_attempted_zone" ]; then
  127. _err "No zone for domain \"$_domain\" found."
  128. return 1
  129. fi
  130. _debug "Looking for zone \"${_attempted_zone}\""
  131. line_num="$(echo "$_zone_names" | grep -n "$_attempted_zone" | cut -d : -f 1)"
  132. if [ "$line_num" ]; then
  133. _zone_id=$(echo "$_zone_ids" | sed -n "${line_num}p")
  134. _debug "Found relevant zone \"$_attempted_zone\" with id \"$_zone_id\" - will be used for domain \"$_domain\"."
  135. return 0
  136. fi
  137. _debug "Zone \"$_attempted_zone\" doesn't exist, let's try a less specific zone."
  138. _strip_counter=$(_math "$_strip_counter" + 1)
  139. done
  140. }
  141. # vim: et:ts=2:sw=2: