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.

197 lines
5.9 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_openprovider_rest_info='OpenProvider (REST)
  4. Site:
  5. OpenProvider.eu
  6. OpenProvider.com
  7. Docs:
  8. github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_openprovider_rest
  9. Options:
  10. OPENPROVIDER_REST_USERNAME Openprovider Account Username
  11. OPENPROVIDER_REST_PASSWORD Openprovider Account Password
  12. Issues:
  13. github.com/acmesh-official/acme.sh/issues/6122
  14. Author:
  15. Lambiek12
  16. '
  17. OPENPROVIDER_API_URL="https://api.openprovider.eu/v1beta"
  18. ######## Public functions #####################
  19. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  20. # Used to add txt record
  21. dns_openprovider_rest_add() {
  22. fulldomain=$1
  23. txtvalue=$2
  24. _openprovider_prepare_credentials || return 1
  25. _debug "Try fetch OpenProvider DNS zone details"
  26. if ! _get_dns_zone "$fulldomain"; then
  27. _err "DNS zone not found within configured OpenProvider account."
  28. return 1
  29. fi
  30. if [ -n "$_domain_id" ]; then
  31. addzonerecordrequestparameters="dns/zones/$_domain_name"
  32. addzonerecordrequestbody="{\"id\":$_domain_id,\"name\":\"$_domain_name\",\"records\":{\"add\":[{\"name\":\"$_sub_domain\",\"ttl\":900,\"type\":\"TXT\",\"value\":\"$txtvalue\"}]}}"
  33. if _openprovider_rest PUT "$addzonerecordrequestparameters" "$addzonerecordrequestbody"; then
  34. if _contains "$response" "\"success\":true"; then
  35. return 0
  36. elif _contains "$response" "\"Duplicate record\""; then
  37. _debug "Record already existed"
  38. return 0
  39. else
  40. _err "Adding TXT record failed due to errors."
  41. return 1
  42. fi
  43. fi
  44. fi
  45. _err "Adding TXT record failed due to errors."
  46. return 1
  47. }
  48. # Usage: rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  49. # Used to remove the txt record after validation
  50. dns_openprovider_rest_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. _openprovider_prepare_credentials || return 1
  54. _debug "Try fetch OpenProvider DNS zone details"
  55. if ! _get_dns_zone "$fulldomain"; then
  56. _err "DNS zone not found within configured OpenProvider account."
  57. return 1
  58. fi
  59. if [ -n "$_domain_id" ]; then
  60. removezonerecordrequestparameters="dns/zones/$_domain_name"
  61. removezonerecordrequestbody="{\"id\":$_domain_id,\"name\":\"$_domain_name\",\"records\":{\"remove\":[{\"name\":\"$_sub_domain\",\"ttl\":900,\"type\":\"TXT\",\"value\":\"\\\"$txtvalue\\\"\"}]}}"
  62. if _openprovider_rest PUT "$removezonerecordrequestparameters" "$removezonerecordrequestbody"; then
  63. if _contains "$response" "\"success\":true"; then
  64. return 0
  65. else
  66. _err "Removing TXT record failed due to errors."
  67. return 1
  68. fi
  69. fi
  70. fi
  71. _err "Removing TXT record failed due to errors."
  72. return 1
  73. }
  74. #################### OpenProvider API common functions ####################
  75. _openprovider_prepare_credentials() {
  76. OPENPROVIDER_REST_USERNAME="${OPENPROVIDER_REST_USERNAME:-$(_readaccountconf_mutable OPENPROVIDER_REST_USERNAME)}"
  77. OPENPROVIDER_REST_PASSWORD="${OPENPROVIDER_REST_PASSWORD:-$(_readaccountconf_mutable OPENPROVIDER_REST_PASSWORD)}"
  78. if [ -z "$OPENPROVIDER_REST_USERNAME" ] || [ -z "$OPENPROVIDER_REST_PASSWORD" ]; then
  79. OPENPROVIDER_REST_USERNAME=""
  80. OPENPROVIDER_REST_PASSWORD=""
  81. _err "You didn't specify the Openprovider username or password yet."
  82. return 1
  83. fi
  84. #save the credentials to the account conf file.
  85. _saveaccountconf_mutable OPENPROVIDER_REST_USERNAME "$OPENPROVIDER_REST_USERNAME"
  86. _saveaccountconf_mutable OPENPROVIDER_REST_PASSWORD "$OPENPROVIDER_REST_PASSWORD"
  87. }
  88. _openprovider_rest() {
  89. httpmethod=$1
  90. queryparameters=$2
  91. requestbody=$3
  92. _openprovider_rest_login
  93. if [ -z "$openproviderauthtoken" ]; then
  94. _err "Unable to fetch authentication token from Openprovider API."
  95. return 1
  96. fi
  97. export _H1="Content-Type: application/json"
  98. export _H2="Accept: application/json"
  99. export _H3="Authorization: Bearer $openproviderauthtoken"
  100. _debug httpmethod "$httpmethod"
  101. _debug requestfullurl "$OPENPROVIDER_API_URL/$queryparameters"
  102. _debug queryparameters "$queryparameters"
  103. if [ "$httpmethod" != "GET" ]; then
  104. _debug requestbody "$requestbody"
  105. response="$(_post "$requestbody" "$OPENPROVIDER_API_URL/$queryparameters" "" "$httpmethod")"
  106. else
  107. response="$(_get "$OPENPROVIDER_API_URL/$queryparameters")"
  108. fi
  109. if [ "$?" != "0" ]; then
  110. _err "No valid parameters supplied for Openprovider API: Error $queryparameters"
  111. return 1
  112. fi
  113. _debug2 response "$response"
  114. return 0
  115. }
  116. _openprovider_rest_login() {
  117. export _H1="Content-Type: application/json"
  118. export _H2="Accept: application/json"
  119. loginrequesturl="$OPENPROVIDER_API_URL/auth/login"
  120. loginrequestbody="{\"ip\":\"0.0.0.0\",\"password\":\"$OPENPROVIDER_REST_PASSWORD\",\"username\":\"$OPENPROVIDER_REST_USERNAME\"}"
  121. loginresponse="$(_post "$loginrequestbody" "$loginrequesturl" "" "POST")"
  122. openproviderauthtoken="$(printf "%s\n" "$loginresponse" | _egrep_o '"token" *: *"[^"]*' | _head_n 1 | sed 's#^"token" *: *"##')"
  123. _debug openproviderauthtoken "$openproviderauthtoken"
  124. export openproviderauthtoken
  125. }
  126. #################### Private functions ##################################
  127. # Usage: _get_dns_zone _acme-challenge.www.domain.com
  128. # Returns:
  129. # _domain_id=123456789
  130. # _domain_name=domain.com
  131. # _sub_domain=_acme-challenge.www
  132. _get_dns_zone() {
  133. domain=$1
  134. i=1
  135. p=1
  136. while true; do
  137. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  138. if [ -z "$h" ]; then
  139. # Empty value not allowed
  140. return 1
  141. fi
  142. if ! _openprovider_rest GET "dns/zones/$h" ""; then
  143. return 1
  144. fi
  145. if _contains "$response" "\"name\":\"$h\""; then
  146. _domain_id="$(printf "%s\n" "$response" | _egrep_o '"id" *: *[^,]*' | _head_n 1 | sed 's#^"id" *: *##')"
  147. _debug _domain_id "$_domain_id"
  148. _domain_name="$h"
  149. _debug _domain_name "$_domain_name"
  150. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  151. _debug _sub_domain "$_sub_domain"
  152. return 0
  153. fi
  154. p=$i
  155. i=$(_math "$i" + 1)
  156. done
  157. return 1
  158. }