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.

245 lines
7.5 KiB

4 years ago
4 years ago
4 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. url="https://management.1984hosting.com/domains"
  83. _htmlget "$url" "$_domain"
  84. _debug2 _response "$_response"
  85. zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+')"
  86. _debug2 zone_id "$zone_id"
  87. if [ -z "$zone_id" ]; then
  88. _err "Error getting zone_id for $1"
  89. return 1
  90. fi
  91. _htmlget "$url/$zone_id" "$_sub_domain"
  92. _debug2 _response "$_response"
  93. entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
  94. _debug2 entry_id "$entry_id"
  95. if [ -z "$entry_id" ]; then
  96. _err "Error getting TXT entry_id for $1"
  97. return 1
  98. fi
  99. _authpost "entry=$entry_id" "$url/delentry/"
  100. response="$(echo "$_response" | _normalizeJson)"
  101. _debug2 response "$response"
  102. if ! _contains "$response" '"ok": true'; then
  103. _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post"
  104. return 1
  105. fi
  106. _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
  107. return 0
  108. }
  109. #################### Private functions below ##################################
  110. # usage: _1984hosting_login username password
  111. # returns 0 success
  112. _1984hosting_login() {
  113. if ! _check_credentials; then return 1; fi
  114. if _check_cookies; then
  115. _debug "Already logged in"
  116. return 0
  117. fi
  118. _debug "Login to 1984Hosting as user $One984HOSTING_Username"
  119. username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
  120. password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
  121. url="https://management.1984hosting.com/accounts/checkuserauth/"
  122. response="$(_post "username=$username&password=$password&otpkey=" $url)"
  123. response="$(echo "$response" | _normalizeJson)"
  124. _debug2 response "$response"
  125. if _contains "$response" '"loggedin": true'; then
  126. One984HOSTING_SESSIONID_COOKIE="$(grep -io 'sessionid=[^;]*;' "$HTTP_HEADER" | tr -d ';')"
  127. One984HOSTING_CSRFTOKEN_COOKIE="$(grep -io 'csrftoken=[^;]*;' "$HTTP_HEADER" | tr -d ';')"
  128. export One984HOSTING_SESSIONID_COOKIE
  129. export One984HOSTING_CSRFTOKEN_COOKIE
  130. _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
  131. _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
  132. return 0
  133. fi
  134. return 1
  135. }
  136. _check_credentials() {
  137. if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
  138. One984HOSTING_Username=""
  139. One984HOSTING_Password=""
  140. _err "You haven't specified 1984Hosting username or password yet."
  141. _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
  142. return 1
  143. fi
  144. return 0
  145. }
  146. _check_cookies() {
  147. One984HOSTING_SESSIONID_COOKIE="${One984HOSTING_SESSIONID_COOKIE:-$(_readaccountconf_mutable One984HOSTING_SESSIONID_COOKIE)}"
  148. One984HOSTING_CSRFTOKEN_COOKIE="${One984HOSTING_CSRFTOKEN_COOKIE:-$(_readaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE)}"
  149. if [ -z "$One984HOSTING_SESSIONID_COOKIE" ] || [ -z "$One984HOSTING_CSRFTOKEN_COOKIE" ]; then
  150. _debug "No cached cookie(s) found"
  151. return 1
  152. fi
  153. _authget "https://management.1984hosting.com/accounts/loginstatus/"
  154. if _contains "$response" '"ok": true'; then
  155. _debug "Cached cookies still valid"
  156. return 0
  157. fi
  158. _debug "Cached cookies no longer valid"
  159. One984HOSTING_SESSIONID_COOKIE=""
  160. One984HOSTING_CSRFTOKEN_COOKIE=""
  161. _saveaccountconf_mutable One984HOSTING_SESSIONID_COOKIE "$One984HOSTING_SESSIONID_COOKIE"
  162. _saveaccountconf_mutable One984HOSTING_CSRFTOKEN_COOKIE "$One984HOSTING_CSRFTOKEN_COOKIE"
  163. return 1
  164. }
  165. #_acme-challenge.www.domain.com
  166. #returns
  167. # _sub_domain=_acme-challenge.www
  168. # _domain=domain.com
  169. _get_root() {
  170. domain="$1"
  171. i=1
  172. p=1
  173. while true; do
  174. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  175. if [ -z "$h" ]; then
  176. #not valid
  177. return 1
  178. fi
  179. _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
  180. if _contains "$_response" "serial" && ! _contains "$_response" "null"; then
  181. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  182. _domain="$h"
  183. return 0
  184. fi
  185. p=$i
  186. i=$(_math "$i" + 1)
  187. done
  188. return 1
  189. }
  190. # add extra headers to request
  191. _authget() {
  192. export _H1="Cookie: $One984HOSTING_SESSIONID_COOKIE"
  193. export _H2="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE"
  194. _response=$(_get "$1" | _normalizeJson)
  195. _debug2 _response "$_response"
  196. }
  197. # truncate huge HTML response
  198. # echo: Argument list too long
  199. _htmlget() {
  200. export _H1="Cookie: $One984HOSTING_SESSIONID_COOKIE"
  201. export _H2="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE"
  202. _response=$(_get "$1" | grep "$2" | _head_n 1)
  203. }
  204. # add extra headers to request
  205. _authpost() {
  206. export _H1="Cookie: $One984HOSTING_SESSIONID_COOKIE"
  207. export _H2="Cookie: $One984HOSTING_CSRFTOKEN_COOKIE"
  208. _response=$(_post "$1" "$2")
  209. }