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.

252 lines
7.8 KiB

3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env sh
  2. #This file name is "dns_1984hosting.sh"
  3. #So, here must be a method dns_1984hosting_add()
  4. #Which will be called by acme.sh to add the txt record to your api system.
  5. #returns 0 means success, otherwise error.
  6. #Author: Adrian Fedoreanu
  7. #Report Bugs here: https://github.com/acmesh-official/acme.sh
  8. # or here... https://github.com/acmesh-official/acme.sh/issues/2851
  9. #
  10. ######## Public functions #####################
  11. # Export 1984HOSTING username and password in following variables
  12. #
  13. # One984HOSTING_Username=username
  14. # One984HOSTING_Password=password
  15. #
  16. # sessionid cookie is saved in ~/.acme.sh/account.conf
  17. # username/password need to be set only when changed.
  18. #Usage: dns_1984hosting_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  19. dns_1984hosting_add() {
  20. fulldomain=$1
  21. txtvalue=$2
  22. _info "Add TXT record using 1984Hosting"
  23. _debug fulldomain "$fulldomain"
  24. _debug txtvalue "$txtvalue"
  25. if ! _1984hosting_login; then
  26. _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
  27. return 1
  28. fi
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain" "$fulldomain"
  32. return 1
  33. fi
  34. _debug _sub_domain "$_sub_domain"
  35. _debug _domain "$_domain"
  36. _debug "Add TXT record $fulldomain with value '$txtvalue'"
  37. value="$(printf '%s' "$txtvalue" | _url_encode)"
  38. url="https://management.1984hosting.com/domains/entry/"
  39. postdata="entry=new"
  40. postdata="$postdata&type=TXT"
  41. postdata="$postdata&ttl=3600"
  42. postdata="$postdata&zone=$_domain"
  43. postdata="$postdata&host=$_sub_domain"
  44. postdata="$postdata&rdata=%22$value%22"
  45. _debug2 postdata "$postdata"
  46. _authpost "$postdata" "$url"
  47. response="$(echo "$_response" | _normalizeJson)"
  48. _debug2 response "$response"
  49. if _contains "$response" '"haserrors": true'; then
  50. _err "1984Hosting failed to add TXT record for $_sub_domain bad RC from _post"
  51. return 1
  52. elif _contains "$response" "html>"; then
  53. _err "1984Hosting failed to add TXT record for $_sub_domain. Check $HTTP_HEADER file"
  54. return 1
  55. elif _contains "$response" '"auth": false'; then
  56. _err "1984Hosting failed to add TXT record for $_sub_domain. Invalid or expired cookie"
  57. return 1
  58. fi
  59. _info "Added acme challenge TXT record for $fulldomain at 1984Hosting"
  60. return 0
  61. }
  62. #Usage: fulldomain txtvalue
  63. #Remove the txt record after validation.
  64. dns_1984hosting_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. _info "Delete TXT record using 1984Hosting"
  68. _debug fulldomain "$fulldomain"
  69. _debug txtvalue "$txtvalue"
  70. if ! _1984hosting_login; then
  71. _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
  72. return 1
  73. fi
  74. _debug "First detect the root zone"
  75. if ! _get_root "$fulldomain"; then
  76. _err "invalid domain" "$fulldomain"
  77. return 1
  78. fi
  79. _debug _sub_domain "$_sub_domain"
  80. _debug _domain "$_domain"
  81. _debug "Delete $fulldomain TXT record"
  82. _htmlget "$url/$zone_id" "$_sub_domain"
  83. _debug2 _response "$_response"
  84. entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
  85. _debug2 entry_id "$entry_id"
  86. if [ -z "$entry_id" ]; then
  87. _err "Error getting TXT entry_id for $1"
  88. return 1
  89. fi
  90. _authpost "entry=$entry_id" "$url/delentry/"
  91. response="$(echo "$_response" | _normalizeJson)"
  92. _debug2 response "$response"
  93. if ! _contains "$response" '"ok": true'; then
  94. _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post"
  95. return 1
  96. fi
  97. _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
  98. return 0
  99. }
  100. #################### Private functions below ##################################
  101. # usage: _1984hosting_login username password
  102. # returns 0 success
  103. _1984hosting_login() {
  104. if ! _check_credentials; then return 1; fi
  105. if _check_cookies; then
  106. _debug "Already logged in"
  107. return 0
  108. fi
  109. _debug "Login to 1984Hosting as user $One984HOSTING_Username"
  110. username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
  111. password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
  112. url="https://management.1984hosting.com/accounts/checkuserauth/"
  113. response="$(_post "username=$username&password=$password&otpkey=" $url)"
  114. response="$(echo "$response" | _normalizeJson)"
  115. _debug2 response "$response"
  116. if _contains "$response" '"loggedin": true'; then
  117. One984HOSTING_SESSIONID_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
  118. One984HOSTING_CSRFTOKEN_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _egrep_o 'csrftoken=[^;]*;' | tr -d ';')"
  119. export One984HOSTING_SESSIONID_COOKIE
  120. export One984HOSTING_CSRFTOKEN_COOKIE
  121. _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
  122. _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
  123. return 0
  124. fi
  125. return 1
  126. }
  127. _check_credentials() {
  128. if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
  129. One984HOSTING_Username=""
  130. One984HOSTING_Password=""
  131. _err "You haven't specified 1984Hosting username or password yet."
  132. _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
  133. return 1
  134. fi
  135. return 0
  136. }
  137. _check_cookies() {
  138. One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
  139. One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
  140. if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
  141. _debug "No cached cookie(s) found"
  142. return 1
  143. fi
  144. _authget "https://management.1984hosting.com/accounts/loginstatus/"
  145. if _contains "$response" '"ok": true'; then
  146. _debug "Cached cookies still valid"
  147. return 0
  148. fi
  149. _debug "Cached cookies no longer valid"
  150. One984HOSTING_SESSIONID_COOKIE=""
  151. One984HOSTING_CSRFTOKEN_COOKIE=""
  152. _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
  153. _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
  154. return 1
  155. }
  156. #_acme-challenge.www.domain.com
  157. #returns
  158. # _sub_domain=_acme-challenge.www
  159. # _domain=domain.com
  160. _get_root() {
  161. domain="$1"
  162. i=1
  163. p=1
  164. while true; do
  165. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  166. if [ -z "$h" ]; then
  167. #not valid
  168. return 1
  169. fi
  170. _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
  171. if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
  172. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  173. _domain="$h"
  174. return 0
  175. fi
  176. p=$i
  177. i=$(_math "$i" + 1)
  178. done
  179. return 1
  180. }
  181. #domain.com
  182. #returns zone id for domain.com
  183. _get_zone_id() {
  184. url="https://management.1984hosting.com/domains"
  185. _htmlget "$url" "$_domain"
  186. _debug2 _response "$_response"
  187. _zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+')"
  188. _debug2 _zone_id "$_zone_id"
  189. if [ -z "$zone_id" ]; then
  190. _err "Error getting _zone_id for $1"
  191. return 1
  192. fi
  193. return 0
  194. }
  195. # add extra headers to request
  196. _authget() {
  197. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
  198. _response=$(_get "$1" | _normalizeJson)
  199. _debug2 _response "$_response"
  200. }
  201. # truncate huge HTML response
  202. # echo: Argument list too long
  203. _htmlget() {
  204. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
  205. _response=$(_get "$1" | grep "$2" | _head_n 1)
  206. }
  207. # add extra headers to request
  208. _authpost() {
  209. _get_zone_id "$@"
  210. csrf_header="$(echo "$One984HOSTING_CSRFTOKEN_COOKIE" | _egrep_o "=[^=][0-9a-zA-Z]*" | tr -d "=")"
  211. export _H1="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE;$One984HOSTING_SESSIONID_COOKIE"
  212. export _H2="Referer: https://management.1984hosting.com/domains/$_zone_id"
  213. export _H3="X-CSRFToken: $csrf_header"
  214. _response=$(_post "$1" "$2")
  215. }