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.

236 lines
6.8 KiB

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. _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_cookie; 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_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _tail_n 1 | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
  127. export One984HOSTING_COOKIE
  128. _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
  129. return 0
  130. fi
  131. return 1
  132. }
  133. _check_credentials() {
  134. if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
  135. One984HOSTING_Username=""
  136. One984HOSTING_Password=""
  137. _err "You haven't specified 1984Hosting username or password yet."
  138. _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
  139. return 1
  140. fi
  141. return 0
  142. }
  143. _check_cookie() {
  144. One984HOSTING_COOKIE="${One984HOSTING_COOKIE:-$(_readaccountconf_mutable One984HOSTING_COOKIE)}"
  145. if [ -z "$One984HOSTING_COOKIE" ]; then
  146. _debug "No cached cookie found"
  147. return 1
  148. fi
  149. _authget "https://management.1984hosting.com/accounts/loginstatus/"
  150. if _contains "$response" '"ok": true'; then
  151. _debug "Cached cookie still valid"
  152. return 0
  153. fi
  154. _debug "Cached cookie no longer valid"
  155. One984HOSTING_COOKIE=""
  156. _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
  157. return 1
  158. }
  159. #_acme-challenge.www.domain.com
  160. #returns
  161. # _sub_domain=_acme-challenge.www
  162. # _domain=domain.com
  163. _get_root() {
  164. domain="$1"
  165. i=1
  166. p=1
  167. while true; do
  168. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  169. if [ -z "$h" ]; then
  170. #not valid
  171. return 1
  172. fi
  173. _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
  174. if _contains "$_response" "serial" && ! _contains "$_response" 'null}'; then
  175. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  176. _domain="$h"
  177. return 0
  178. fi
  179. p=$i
  180. i=$(_math "$i" + 1)
  181. done
  182. return 1
  183. }
  184. # add extra headers to request
  185. _authget() {
  186. export _H1="Cookie: $One984HOSTING_COOKIE"
  187. _response=$(_get "$1" | _normalizeJson)
  188. _debug2 _response "$_response"
  189. }
  190. # truncate huge HTML response
  191. # echo: Argument list too long
  192. _htmlget() {
  193. export _H1="Cookie: $One984HOSTING_COOKIE"
  194. _response=$(_get "$1" | grep "$2" | _head_n 1)
  195. }
  196. # add extra headers to request
  197. _authpost() {
  198. export _H1="Cookie: $One984HOSTING_COOKIE"
  199. _response=$(_post "$1" "$2")
  200. }