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.

261 lines
8.1 KiB

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