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.

221 lines
7.4 KiB

  1. #!/usr/bin/env sh
  2. ########################################################################
  3. # Geoscaling hook script for acme.sh
  4. #
  5. # Environment variables:
  6. #
  7. # - $GEOSCALING_Username (your Geoscaling username - this is usually NOT an amail address)
  8. # - $GEOSCALING_Password (your Geoscaling password)
  9. #-- dns_geoscaling_add() - Add TXT record --------------------------------------
  10. # Usage: dns_geoscaling_add _acme-challenge.subdomain.domain.com "XyZ123..."
  11. dns_geoscaling_add() {
  12. full_domain=$1
  13. txt_value=$2
  14. _info "Using DNS-01 Geoscaling DNS2 hook"
  15. GEOSCALING_Username="${GEOSCALING_Username:-$(_readaccountconf_mutable GEOSCALING_Username)}"
  16. GEOSCALING_Password="${GEOSCALING_Password:-$(_readaccountconf_mutable GEOSCALING_Password)}"
  17. if [ -z "$GEOSCALING_Username" ] || [ -z "$GEOSCALING_Password" ]; then
  18. GEOSCALING_Username=
  19. GEOSCALING_Password=
  20. _err "No auth details provided. Please set user credentials using the \$GEOSCALING_Username and \$GEOSCALING_Password environment variables."
  21. return 1
  22. fi
  23. _saveaccountconf_mutable GEOSCALING_Username "${GEOSCALING_Username}"
  24. _saveaccountconf_mutable GEOSCALING_Password "${GEOSCALING_Password}"
  25. # Fills in the $zone_id and $zone_name
  26. find_zone "${full_domain}" || return 1
  27. _debug "Zone id '${zone_id}' will be used."
  28. # We're logged in here
  29. # we should add ${full_domain} minus the trailing ${zone_name}
  30. prefix=$(echo "${full_domain}" | sed "s|\\.${zone_name}\$||")
  31. body="id=${zone_id}&name=${prefix}&type=TXT&content=${txt_value}&ttl=300&prio=0"
  32. do_post "$body" "https://www.geoscaling.com/dns2/ajax/add_record.php"
  33. exit_code="$?"
  34. if [ "${exit_code}" -eq 0 ]; then
  35. _info "TXT record added successfully."
  36. else
  37. _err "Couldn't add the TXT record."
  38. fi
  39. do_logout
  40. return "${exit_code}"
  41. }
  42. #-- dns_geoscaling_rm() - Remove TXT record ------------------------------------
  43. # Usage: dns_geoscaling_rm _acme-challenge.subdomain.domain.com "XyZ123..."
  44. dns_geoscaling_rm() {
  45. full_domain=$1
  46. txt_value=$2
  47. _info "Cleaning up after DNS-01 Geoscaling DNS2 hook"
  48. # fills in the $zone_id
  49. find_zone "${full_domain}" || return 1
  50. _debug "Zone id '${zone_id}' will be used."
  51. # Here we're logged in
  52. # Find the record id to clean
  53. # get the domain
  54. response=$(do_get "https://www.geoscaling.com/dns2/index.php?module=domain&id=${zone_id}")
  55. _debug2 "response" "$response"
  56. table="$(echo "${response}" | tr -d '\n' | sed 's|.*<div class="box"><div class="boxtitle">Basic Records</div><div class="boxtext"><table|<table|; s|</table>.*|</table>|')"
  57. _debug2 table "${table}"
  58. names=$(echo "${table}" | _egrep_o 'id="[0-9]+\.name">[^<]*</td>' | sed 's|</td>||; s|.*>||')
  59. ids=$(echo "${table}" | _egrep_o 'id="[0-9]+\.name">[^<]*</td>' | sed 's|\.name">.*||; s|id="||')
  60. types=$(echo "${table}" | _egrep_o 'id="[0-9]+\.type">[^<]*</td>' | sed 's|</td>||; s|.*>||')
  61. values=$(echo "${table}" | _egrep_o 'id="[0-9]+\.content">[^<]*</td>' | sed 's|</td>||; s|.*>||')
  62. _debug2 names "${names}"
  63. _debug2 ids "${ids}"
  64. _debug2 types "${types}"
  65. _debug2 values "${values}"
  66. # look for line whose name is ${full_domain}, whose type is TXT, and whose value is ${txt_value}
  67. line_num="$(echo "${values}" | grep -F -n -- "${txt_value}" | _head_n 1 | cut -d ':' -f 1)"
  68. _debug2 line_num "${line_num}"
  69. found_id=
  70. if [ -n "$line_num" ]; then
  71. type=$(echo "${types}" | sed -n "${line_num}p")
  72. name=$(echo "${names}" | sed -n "${line_num}p")
  73. id=$(echo "${ids}" | sed -n "${line_num}p")
  74. _debug2 type "$type"
  75. _debug2 name "$name"
  76. _debug2 id "$id"
  77. _debug2 full_domain "$full_domain"
  78. if [ "${type}" = "TXT" ] && [ "${name}" = "${full_domain}" ]; then
  79. found_id=${id}
  80. fi
  81. fi
  82. if [ "${found_id}" = "" ]; then
  83. _err "Can not find record id."
  84. return 0
  85. fi
  86. # Remove the record
  87. body="id=${zone_id}&record_id=${found_id}"
  88. response=$(do_post "$body" "https://www.geoscaling.com/dns2/ajax/delete_record.php")
  89. exit_code="$?"
  90. if [ "$exit_code" -eq 0 ]; then
  91. _info "Record removed successfully."
  92. else
  93. _err "Could not clean (remove) up the record. Please go to Geoscaling administration interface and clean it by hand."
  94. fi
  95. do_logout
  96. return "${exit_code}"
  97. }
  98. ########################## PRIVATE FUNCTIONS ###########################
  99. do_get() {
  100. _url=$1
  101. export _H1="Cookie: $geoscaling_phpsessid_cookie"
  102. _get "${_url}"
  103. }
  104. do_post() {
  105. _body=$1
  106. _url=$2
  107. export _H1="Cookie: $geoscaling_phpsessid_cookie"
  108. _post "${_body}" "${_url}"
  109. }
  110. do_login() {
  111. _info "Logging in..."
  112. username_encoded="$(printf "%s" "${GEOSCALING_Username}" | _url_encode)"
  113. password_encoded="$(printf "%s" "${GEOSCALING_Password}" | _url_encode)"
  114. body="username=${username_encoded}&password=${password_encoded}"
  115. response=$(_post "$body" "https://www.geoscaling.com/dns2/index.php?module=auth")
  116. _debug2 response "${response}"
  117. #retcode=$(grep '^HTTP[^ ]*' "${HTTP_HEADER}" | _head_n 1 | _egrep_o '[0-9]+$')
  118. retcode=$(grep '^HTTP[^ ]*' "${HTTP_HEADER}" | _head_n 1 | cut -d ' ' -f 2)
  119. if [ "$retcode" != "302" ]; then
  120. _err "Geoscaling login failed for user ${GEOSCALING_Username}. Check ${HTTP_HEADER} file"
  121. return 1
  122. fi
  123. geoscaling_phpsessid_cookie="$(grep -i '^set-cookie:' "${HTTP_HEADER}" | _egrep_o 'PHPSESSID=[^;]*;' | tr -d ';')"
  124. return 0
  125. }
  126. do_logout() {
  127. _info "Logging out."
  128. response="$(do_get "https://www.geoscaling.com/dns2/index.php?module=auth")"
  129. _debug2 response "$response"
  130. return 0
  131. }
  132. find_zone() {
  133. domain="$1"
  134. # do login
  135. do_login || return 1
  136. # get zones
  137. response="$(do_get "https://www.geoscaling.com/dns2/index.php?module=domains")"
  138. table="$(echo "${response}" | tr -d '\n' | sed 's|.*<div class="box"><div class="boxtitle">Your domains</div><div class="boxtext"><table|<table|; s|</table>.*|</table>|')"
  139. _debug2 table "${table}"
  140. zone_names="$(echo "${table}" | _egrep_o '<b>[^<]*</b>' | sed 's|<b>||;s|</b>||')"
  141. _debug2 _matches "${zone_names}"
  142. # Zone names and zone IDs are in same order
  143. zone_ids=$(echo "${table}" | _egrep_o '<a href=.index\.php\?module=domain&id=[0-9]+. onclick="javascript:show_loader\(\);">' | sed 's|.*id=||;s|. .*||')
  144. _debug2 "These are the zones on this Geoscaling account:"
  145. _debug2 "zone_names" "${zone_names}"
  146. _debug2 "And these are their respective IDs:"
  147. _debug2 "zone_ids" "${zone_ids}"
  148. if [ -z "${zone_names}" ] || [ -z "${zone_ids}" ]; then
  149. _err "Can not get zone names or IDs."
  150. return 1
  151. fi
  152. # Walk through all possible zone names
  153. strip_counter=1
  154. while true; do
  155. attempted_zone=$(echo "${domain}" | cut -d . -f ${strip_counter}-)
  156. # All possible zone names have been tried
  157. if [ -z "${attempted_zone}" ]; then
  158. _err "No zone for domain '${domain}' found."
  159. return 1
  160. fi
  161. _debug "Looking for zone '${attempted_zone}'"
  162. line_num="$(echo "${zone_names}" | grep -n "^${attempted_zone}\$" | _head_n 1 | cut -d : -f 1)"
  163. _debug2 line_num "${line_num}"
  164. if [ "$line_num" ]; then
  165. zone_id=$(echo "${zone_ids}" | sed -n "${line_num}p")
  166. zone_name=$(echo "${zone_names}" | sed -n "${line_num}p")
  167. if [ -z "${zone_id}" ]; then
  168. _err "Can not find zone id."
  169. return 1
  170. fi
  171. _debug "Found relevant zone '${attempted_zone}' with id '${zone_id}' - will be used for domain '${domain}'."
  172. return 0
  173. fi
  174. _debug "Zone '${attempted_zone}' doesn't exist, let's try a less specific zone."
  175. strip_counter=$(_math "${strip_counter}" + 1)
  176. done
  177. }
  178. # vim: et:ts=2:sw=2: