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.

218 lines
5.5 KiB

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