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.

224 lines
5.6 KiB

6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_online_info='online.net
  4. Domains: scaleway.com
  5. Site: online.net
  6. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_online
  7. Options:
  8. ONLINE_API_KEY API Key
  9. Issues: github.com/acmesh-official/acme.sh/issues/2093
  10. '
  11. # Online API
  12. # https://console.online.net/en/api/
  13. ######## Public functions #####################
  14. ONLINE_API="https://api.online.net/api/v1"
  15. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  16. dns_online_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. if ! _online_check_config; then
  20. return 1
  21. fi
  22. _debug "First detect the root zone"
  23. if ! _get_root "$fulldomain"; then
  24. _err "invalid domain"
  25. return 1
  26. fi
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _debug _real_dns_version "$_real_dns_version"
  30. _info "Creating temporary zone version"
  31. _online_create_temporary_zone_version
  32. _info "Enabling temporary zone version"
  33. _online_enable_zone "$_temporary_dns_version"
  34. _info "Adding record"
  35. _online_create_TXT_record "$_real_dns_version" "$_sub_domain" "$txtvalue"
  36. _info "Disabling temporary version"
  37. _online_enable_zone "$_real_dns_version"
  38. _info "Destroying temporary version"
  39. _online_destroy_zone "$_temporary_dns_version"
  40. _info "Record added."
  41. return 0
  42. }
  43. #fulldomain
  44. dns_online_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. if ! _online_check_config; then
  48. return 1
  49. fi
  50. _debug "First detect the root zone"
  51. if ! _get_root "$fulldomain"; then
  52. _err "invalid domain"
  53. return 1
  54. fi
  55. _debug _sub_domain "$_sub_domain"
  56. _debug _domain "$_domain"
  57. _debug _real_dns_version "$_real_dns_version"
  58. _debug "Getting txt records"
  59. if ! _online_rest GET "domain/$_domain/version/active"; then
  60. return 1
  61. fi
  62. rid=$(echo "$response" | _egrep_o "\"id\":[0-9]+,\"name\":\"$_sub_domain\",\"data\":\"\\\u0022$txtvalue\\\u0022\"" | cut -d ':' -f 2 | cut -d ',' -f 1)
  63. _debug rid "$rid"
  64. if [ -z "$rid" ]; then
  65. return 1
  66. fi
  67. _info "Creating temporary zone version"
  68. _online_create_temporary_zone_version
  69. _info "Enabling temporary zone version"
  70. _online_enable_zone "$_temporary_dns_version"
  71. _info "Removing DNS record"
  72. _online_rest DELETE "domain/$_domain/version/$_real_dns_version/zone/$rid"
  73. _info "Disabling temporary version"
  74. _online_enable_zone "$_real_dns_version"
  75. _info "Destroying temporary version"
  76. _online_destroy_zone "$_temporary_dns_version"
  77. return 0
  78. }
  79. #################### Private functions below ##################################
  80. _online_check_config() {
  81. ONLINE_API_KEY="${ONLINE_API_KEY:-$(_readaccountconf_mutable ONLINE_API_KEY)}"
  82. if [ -z "$ONLINE_API_KEY" ]; then
  83. _err "No API key specified for Online API."
  84. _err "Create your key and export it as ONLINE_API_KEY"
  85. return 1
  86. fi
  87. if ! _online_rest GET "domain/"; then
  88. _err "Invalid API key specified for Online API."
  89. return 1
  90. fi
  91. _saveaccountconf_mutable ONLINE_API_KEY "$ONLINE_API_KEY"
  92. return 0
  93. }
  94. #_acme-challenge.www.domain.com
  95. #returns
  96. # _sub_domain=_acme-challenge.www
  97. # _domain=domain.com
  98. _get_root() {
  99. domain=$1
  100. i=2
  101. p=1
  102. while true; do
  103. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  104. if [ -z "$h" ]; then
  105. #not valid
  106. return 1
  107. fi
  108. _online_rest GET "domain/$h/version/active"
  109. if ! _contains "$response" "Domain not found" >/dev/null; then
  110. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  111. _domain="$h"
  112. _real_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  113. return 0
  114. fi
  115. p=$i
  116. i=$(_math "$i" + 1)
  117. done
  118. _err "Unable to retrive DNS zone matching this domain"
  119. return 1
  120. }
  121. # this function create a temporary zone version
  122. # as online.net does not allow updating an active version
  123. _online_create_temporary_zone_version() {
  124. _online_rest POST "domain/$_domain/version" "name=acme.sh"
  125. if [ "$?" != "0" ]; then
  126. return 1
  127. fi
  128. _temporary_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  129. # Creating a dummy record in this temporary version, because online.net doesn't accept enabling an empty version
  130. _online_create_TXT_record "$_temporary_dns_version" "dummy.acme.sh" "dummy"
  131. return 0
  132. }
  133. _online_destroy_zone() {
  134. version_id=$1
  135. _online_rest DELETE "domain/$_domain/version/$version_id"
  136. if [ "$?" != "0" ]; then
  137. return 1
  138. fi
  139. return 0
  140. }
  141. _online_enable_zone() {
  142. version_id=$1
  143. _online_rest PATCH "domain/$_domain/version/$version_id/enable"
  144. if [ "$?" != "0" ]; then
  145. return 1
  146. fi
  147. return 0
  148. }
  149. _online_create_TXT_record() {
  150. version=$1
  151. txt_name=$2
  152. txt_value=$3
  153. _online_rest POST "domain/$_domain/version/$version/zone" "type=TXT&name=$txt_name&data=%22$txt_value%22&ttl=60&priority=0"
  154. # Note : the normal, expected response SHOULD be "Unknown method".
  155. # this happens because the API HTTP response contains a Location: header, that redirect
  156. # to an unknown online.net endpoint.
  157. if [ "$?" != "0" ] || _contains "$response" "Unknown method" || _contains "$response" "\$ref"; then
  158. return 0
  159. else
  160. _err "error $response"
  161. return 1
  162. fi
  163. }
  164. _online_rest() {
  165. m=$1
  166. ep="$2"
  167. data="$3"
  168. _debug "$ep"
  169. _online_url="$ONLINE_API/$ep"
  170. _debug2 _online_url "$_online_url"
  171. export _H1="Authorization: Bearer $ONLINE_API_KEY"
  172. export _H2="X-Pretty-JSON: 1"
  173. if [ "$data" ] || [ "$m" != "GET" ]; then
  174. _debug data "$data"
  175. response="$(_post "$data" "$_online_url" "" "$m")"
  176. else
  177. response="$(_get "$_online_url")"
  178. fi
  179. if [ "$?" != "0" ] || _contains "$response" "invalid_grant" || _contains "$response" "Method not allowed"; then
  180. _err "error $response"
  181. return 1
  182. fi
  183. _debug2 response "$response"
  184. return 0
  185. }