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