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.

214 lines
5.3 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. if [ -z "$ONLINE_API_KEY" ]; then
  75. _err "No API key specified for Online API."
  76. _err "Create your key and export it as ONLINE_API_KEY"
  77. return 1
  78. fi
  79. _saveaccountconf ONLINE_API_KEY "$ONLINE_API_KEY"
  80. return 0
  81. }
  82. #_acme-challenge.www.domain.com
  83. #returns
  84. # _sub_domain=_acme-challenge.www
  85. # _domain=domain.com
  86. _get_root() {
  87. domain=$1
  88. i=2
  89. p=1
  90. while true; do
  91. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  92. if [ -z "$h" ]; then
  93. #not valid
  94. return 1
  95. fi
  96. if ! _online_rest GET "domain/$h/version/active"; then
  97. _err "Unable to retrive DNS zone matching this domain"
  98. return 1
  99. fi
  100. if ! _contains "$response" "Domain not found" >/dev/null; then
  101. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  102. _domain="$h"
  103. _real_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  104. return 0
  105. fi
  106. p=$i
  107. i=$(_math "$i" + 1)
  108. done
  109. return 1
  110. }
  111. # this function create a temporary zone version
  112. # as online.net does not allow updating an active version
  113. _online_create_temporary_zone_version() {
  114. _online_rest POST "domain/$_domain/version" "name=acme.sh"
  115. if [ "$?" != "0" ]; then
  116. return 1
  117. fi
  118. _temporary_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  119. # Creating a dummy record in this temporary version, because online.net doesn't accept enabling an empty version
  120. _online_create_TXT_record "$_temporary_dns_version" "dummy.acme.sh" "dummy"
  121. return 0
  122. }
  123. _online_destroy_zone() {
  124. version_id=$1
  125. _online_rest DELETE "domain/$_domain/version/$version_id"
  126. if [ "$?" != "0" ]; then
  127. return 1
  128. fi
  129. return 0
  130. }
  131. _online_enable_zone() {
  132. version_id=$1
  133. _online_rest PATCH "domain/$_domain/version/$version_id/enable"
  134. if [ "$?" != "0" ]; then
  135. return 1
  136. fi
  137. return 0
  138. }
  139. _online_create_TXT_record() {
  140. version=$1
  141. txt_name=$2
  142. txt_value=$3
  143. _online_rest POST "domain/$_domain/version/$version/zone" "type=TXT&name=$txt_name&data=%22$txt_value%22&ttl=60&priority=0"
  144. # Note : the normal, expected response SHOULD be "Unknown method".
  145. # this happens because the API HTTP response contains a Location: header, that redirect
  146. # to an unknown online.net endpoint.
  147. if [ "$?" != "0" ] || _contains "$response" "Unknown method"; then
  148. return 0
  149. else
  150. _err "error $response"
  151. return 1
  152. fi
  153. }
  154. _online_rest() {
  155. m=$1
  156. ep="$2"
  157. data="$3"
  158. _debug "$ep"
  159. _online_url="$ONLINE_API/$ep"
  160. _debug2 _online_url "$_online_url"
  161. export _H1="Authorization: Bearer $ONLINE_API_KEY"
  162. export _H2="X-Pretty-JSON: 1"
  163. if [ "$data" ] || [ "$m" = "PATCH" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  164. _debug data "$data"
  165. response="$(_post "$data" "$_online_url" "" "$m")"
  166. else
  167. response="$(_get "$_online_url")"
  168. fi
  169. if [ "$?" != "0" ] || _contains "$response" "invalid_grant" || _contains "$response" "Method not allowed"; then
  170. _err "error $response"
  171. return 1
  172. fi
  173. _debug2 response "$response"
  174. return 0
  175. }