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.

217 lines
5.4 KiB

6 years ago
6 years ago
6 years ago
  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="${ONLINE_API_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. _online_rest GET "domain/$h/version/active"
  102. if ! _contains "$response" "Domain not found" >/dev/null; then
  103. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  104. _domain="$h"
  105. _real_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  106. return 0
  107. fi
  108. p=$i
  109. i=$(_math "$i" + 1)
  110. done
  111. _err "Unable to retrive DNS zone matching this domain"
  112. return 1
  113. }
  114. # this function create a temporary zone version
  115. # as online.net does not allow updating an active version
  116. _online_create_temporary_zone_version() {
  117. _online_rest POST "domain/$_domain/version" "name=acme.sh"
  118. if [ "$?" != "0" ]; then
  119. return 1
  120. fi
  121. _temporary_dns_version=$(echo "$response" | _egrep_o '"uuid_ref":.*' | cut -d ':' -f 2 | cut -d '"' -f 2)
  122. # Creating a dummy record in this temporary version, because online.net doesn't accept enabling an empty version
  123. _online_create_TXT_record "$_temporary_dns_version" "dummy.acme.sh" "dummy"
  124. return 0
  125. }
  126. _online_destroy_zone() {
  127. version_id=$1
  128. _online_rest DELETE "domain/$_domain/version/$version_id"
  129. if [ "$?" != "0" ]; then
  130. return 1
  131. fi
  132. return 0
  133. }
  134. _online_enable_zone() {
  135. version_id=$1
  136. _online_rest PATCH "domain/$_domain/version/$version_id/enable"
  137. if [ "$?" != "0" ]; then
  138. return 1
  139. fi
  140. return 0
  141. }
  142. _online_create_TXT_record() {
  143. version=$1
  144. txt_name=$2
  145. txt_value=$3
  146. _online_rest POST "domain/$_domain/version/$version/zone" "type=TXT&name=$txt_name&data=%22$txt_value%22&ttl=60&priority=0"
  147. # Note : the normal, expected response SHOULD be "Unknown method".
  148. # this happens because the API HTTP response contains a Location: header, that redirect
  149. # to an unknown online.net endpoint.
  150. if [ "$?" != "0" ] || _contains "$response" "Unknown method" || _contains "$response" "\$ref"; then
  151. return 0
  152. else
  153. _err "error $response"
  154. return 1
  155. fi
  156. }
  157. _online_rest() {
  158. m=$1
  159. ep="$2"
  160. data="$3"
  161. _debug "$ep"
  162. _online_url="$ONLINE_API/$ep"
  163. _debug2 _online_url "$_online_url"
  164. export _H1="Authorization: Bearer $ONLINE_API_KEY"
  165. export _H2="X-Pretty-JSON: 1"
  166. if [ "$data" ] || [ "$m" != "GET" ]; then
  167. _debug data "$data"
  168. response="$(_post "$data" "$_online_url" "" "$m")"
  169. else
  170. response="$(_get "$_online_url")"
  171. fi
  172. if [ "$?" != "0" ] || _contains "$response" "invalid_grant" || _contains "$response" "Method not allowed"; then
  173. _err "error $response"
  174. return 1
  175. fi
  176. _debug2 response "$response"
  177. return 0
  178. }