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.1 KiB

  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. _1984hosting_add_txt_record "$_domain" "$_sub_domain" "$txtvalue"
  37. return $?
  38. }
  39. #Usage: fulldomain txtvalue
  40. #Remove the txt record after validation.
  41. dns_1984hosting_rm() {
  42. fulldomain=$1
  43. txtvalue=$2
  44. _info "Delete TXT record using 1984Hosting"
  45. _debug fulldomain "$fulldomain"
  46. _debug txtvalue "$txtvalue"
  47. if ! _1984hosting_login; then
  48. _err "1984Hosting login failed for user $One984HOSTING_Username. Check $HTTP_HEADER file"
  49. return 1
  50. fi
  51. _debug "First detect the root zone"
  52. if ! _get_root "$fulldomain"; then
  53. _err "invalid domain" "$fulldomain"
  54. return 1
  55. fi
  56. _debug _sub_domain "$_sub_domain"
  57. _debug _domain "$_domain"
  58. _1984hosting_delete_txt_record "$_domain" "$_sub_domain"
  59. return $?
  60. }
  61. #################### Private functions below ##################################
  62. # usage _1984hosting_add_txt_record domain subdomain value
  63. # returns 0 success
  64. _1984hosting_add_txt_record() {
  65. _debug "Add TXT record $1 with value '$3'"
  66. domain="$1"
  67. subdomain="$2"
  68. value="$(printf '%s' "$3" | _url_encode)"
  69. url="https://management.1984hosting.com/domains/entry/"
  70. postdata="entry=new"
  71. postdata="$postdata&type=TXT"
  72. postdata="$postdata&ttl=3600"
  73. postdata="$postdata&zone=$domain"
  74. postdata="$postdata&host=$subdomain"
  75. postdata="$postdata&rdata=%22$value%22"
  76. _debug2 postdata "$postdata"
  77. _authpost "$postdata" "$url"
  78. response="$(echo "$_response" | _normalizeJson)"
  79. _debug2 response "$response"
  80. if _contains "$response" '"haserrors": true'; then
  81. _err "1984Hosting failed to add TXT record for $subdomain bad RC from _post"
  82. return 1
  83. elif _contains "$response" "<html>"; then
  84. _err "1984Hosting failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
  85. return 1
  86. elif _contains "$response" '"auth": false'; then
  87. _err "1984Hosting failed to add TXT record for $subdomain. Invalid or expired cookie"
  88. return 1
  89. fi
  90. _info "Added acme challenge TXT record for $fulldomain at 1984Hosting"
  91. return 0
  92. }
  93. # usage _1984hosting_delete_txt_record entry_id
  94. # returns 0 success
  95. _1984hosting_delete_txt_record() {
  96. _debug "Delete $fulldomain TXT record"
  97. domain="$1"
  98. subdomain="$2"
  99. url="https://management.1984hosting.com/domains"
  100. _htmlget "$url" "$domain"
  101. _debug2 _response "$_response"
  102. zone_id="$(echo "$_response" | _egrep_o 'zone\/[0-9]+')"
  103. _debug2 zone_id "$zone_id"
  104. if [ -z "$zone_id" ]; then
  105. _err "Error getting zone_id for $1"
  106. return 1
  107. fi
  108. _htmlget "$url/$zone_id" "$subdomain"
  109. _debug2 _response "$_response"
  110. entry_id="$(echo "$_response" | _egrep_o 'entry_[0-9]+' | sed 's/entry_//')"
  111. _debug2 entry_id "$entry_id"
  112. if [ -z "$entry_id" ]; then
  113. _err "Error getting TXT entry_id for $1"
  114. return 1
  115. fi
  116. _authpost "entry=$entry_id" "$url/delentry/"
  117. response="$(echo "$_response" | _normalizeJson)"
  118. _debug2 response "$response"
  119. if ! _contains "$response" '"ok": true'; then
  120. _err "1984Hosting failed to delete TXT record for $entry_id bad RC from _post"
  121. return 1
  122. fi
  123. _info "Deleted acme challenge TXT record for $fulldomain at 1984Hosting"
  124. return 0
  125. }
  126. # usage: _1984hosting_login username password
  127. # returns 0 success
  128. _1984hosting_login() {
  129. if ! _check_credentials; then return 1; fi
  130. if _check_cookie; then
  131. _debug "Already logged in"
  132. return 0
  133. fi
  134. _debug "Login to 1984Hosting as user $One984HOSTING_Username"
  135. username=$(printf '%s' "$One984HOSTING_Username" | _url_encode)
  136. password=$(printf '%s' "$One984HOSTING_Password" | _url_encode)
  137. url="https://management.1984hosting.com/accounts/checkuserauth/"
  138. response="$(_post "username=$username&password=$password&otpkey=" "$url")"
  139. response="$(echo "$response" | _normalizeJson)"
  140. _debug2 response "$response"
  141. if _contains "$response" '"loggedin": true'; then
  142. One984HOSTING_COOKIE="$(grep -i '^set-cookie:' "$HTTP_HEADER" | _tail_n 1 | _egrep_o 'sessionid=[^;]*;' | tr -d ';')"
  143. export One984HOSTING_COOKIE
  144. _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
  145. return 0
  146. fi
  147. return 1
  148. }
  149. _check_credentials() {
  150. if [ -z "$One984HOSTING_Username" ] || [ -z "$One984HOSTING_Password" ]; then
  151. One984HOSTING_Username=""
  152. One984HOSTING_Password=""
  153. _err "You haven't specified 1984Hosting username or password yet."
  154. _err "Please export as One984HOSTING_Username / One984HOSTING_Password and try again."
  155. return 1
  156. fi
  157. return 0
  158. }
  159. _check_cookie() {
  160. One984HOSTING_COOKIE="${One984HOSTING_COOKIE:-$(_readaccountconf_mutable One984HOSTING_COOKIE)}"
  161. if [ -z "$One984HOSTING_COOKIE" ]; then
  162. _debug "No cached cookie found"
  163. return 1
  164. fi
  165. _authget "https://management.1984hosting.com/accounts/loginstatus/"
  166. response="$(echo "$_response" | _normalizeJson)"
  167. if _contains "$response" '"ok": true'; then
  168. _debug "Cached cookie still valid"
  169. return 0
  170. fi
  171. _debug "Cached cookie no longer valid"
  172. One984HOSTING_COOKIE=""
  173. _saveaccountconf_mutable One984HOSTING_COOKIE "$One984HOSTING_COOKIE"
  174. return 1
  175. }
  176. #_acme-challenge.www.domain.com
  177. #returns
  178. # _sub_domain=_acme-challenge.www
  179. # _domain=domain.com
  180. _get_root() {
  181. domain="$1"
  182. i=2
  183. p=1
  184. while true; do
  185. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  186. if [ -z "$h" ]; then
  187. #not valid
  188. return 1
  189. fi
  190. _authget "https://management.1984hosting.com/domains/soacheck/?zone=$h&nameserver=ns0.1984.is."
  191. if _contains "$_response" "serial"; then
  192. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  193. _domain="$h"
  194. return 0
  195. fi
  196. p=$i
  197. i=$(_math "$i" + 1)
  198. done
  199. return 1
  200. }
  201. # add extra headers to request
  202. _authget() {
  203. export _H1="Cookie: $One984HOSTING_COOKIE"
  204. _response=$(_get "$1")
  205. }
  206. # truncate huge HTML response
  207. # echo: Argument list too long
  208. _htmlget() {
  209. export _H1="Cookie: $One984HOSTING_COOKIE"
  210. _response=$(_get "$1" | grep "$2" | _head_n 1)
  211. }
  212. # add extra headers to request
  213. _authpost() {
  214. export _H1="Cookie: $One984HOSTING_COOKIE"
  215. _response=$(_post "$1" "$2")
  216. }