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.

254 lines
7.9 KiB

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