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.

177 lines
4.4 KiB

  1. #!/usr/bin/env sh
  2. #This is the clodo.ru api wrapper for acme.sh
  3. #
  4. #Author: Oleg Zaikin <zord@mail.ru>
  5. #Report Bugs here: https://github.com/zord1k/acme.sh
  6. #
  7. #CLODO_User="jdoe@example.com"
  8. #CLODO_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  9. #
  10. CLODO_AUTH_URL="https://api.clodo.ru"
  11. CLODO_API=""
  12. CLODO_TOKEN=""
  13. ######## Public functions #####################
  14. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. # Used to add txt record
  16. dns_clodo_add() {
  17. _clodo_init
  18. fulldomain=$1
  19. txtvalue=$2
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _domain_id "$_domain_id"
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _info "Adding record"
  29. if _clodo_rest POST "dns/$_domain_id" "{\"name_0\":\"$_sub_domain\",\"type_0\":\"TXT\",\"content_0\":\"$txtvalue\",\"ttl_0\":120}"; then
  30. if printf -- "%s" "$response" | grep "\"name\":\"$fulldomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\"" >/dev/null; then
  31. _info "Added, OK"
  32. return 0
  33. else
  34. _err "Add txt record error."
  35. return 1
  36. fi
  37. fi
  38. _err "Add txt record error."
  39. return 1
  40. }
  41. # Usage: fulldomain txtvalue
  42. # Used to remove the txt record after validation
  43. dns_clodo_rm() {
  44. _clodo_init
  45. fulldomain=$1
  46. txtvalue=$2
  47. _debug "First detect the root zone"
  48. if ! _get_root "$fulldomain"; then
  49. _err "invalid domain"
  50. return 1
  51. fi
  52. _debug _domain_id "$_domain_id"
  53. _debug _sub_domain "$_sub_domain"
  54. _debug _domain "$_domain"
  55. _debug "Getting txt records"
  56. if ! _clodo_rest GET "dns/$_domain_id"; then
  57. _err "Error"
  58. return 1
  59. fi
  60. if ! _contains "$response" "$txtvalue"; then
  61. _info "Don't need to remove."
  62. else
  63. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\",\"domain_id\":\"$_domain_id\",\"name\":\"$fulldomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\"" | cut -d : -f 2 | cut -d , -f 1 | tr -d \" | head -n 1)
  64. _debug "record_id" "$record_id"
  65. if [ -z "$record_id" ]; then
  66. _err "Can not get record id to remove."
  67. return 1
  68. fi
  69. if ! _clodo_rest DELETE "dns/$_domain_id" "{\"delete_record\":$record_id}"; then
  70. _err "Delete record error."
  71. return 1
  72. fi
  73. _contains "$response" 'new_soa'
  74. fi
  75. }
  76. #################### Private functions below ##################################
  77. _clodo_init() {
  78. CLODO_User="${CLODO_User:-$(_readaccountconf_mutable CLODO_User)}"
  79. CLODO_Key="${CLODO_Key:-$(_readaccountconf_mutable CLODO_Key)}"
  80. if [ -z "$CLODO_User" ] || [ -z "$CLODO_Key" ]; then
  81. CLODO_User=""
  82. CLODO_Key=""
  83. _err "You didn't specify a Clodo user and api key yet."
  84. _err "Please create the key and try again."
  85. return 1
  86. fi
  87. #save the api user and key to the account conf file.
  88. _saveaccountconf_mutable CLODO_User "$CLODO_User"
  89. _saveaccountconf_mutable CLODO_Key "$CLODO_Key"
  90. export _H1="X-Auth-User: $CLODO_User"
  91. export _H2="X-Auth-Key: $CLODO_Key"
  92. _get $CLODO_AUTH_URL "onlyheader"
  93. CLODO_TOKEN=$(grep "^X-Auth-Token" $HTTP_HEADER | tr -d "\r" | cut -d " " -f 2)
  94. CLODO_API=$(grep "^X-Server-Management-Url" $HTTP_HEADER | tr -d "\r" | cut -d " " -f 2)
  95. if [ -z "$CLODO_TOKEN" ] || [ -z "$CLODO_API" ]; then
  96. _err "Authentication error"
  97. return 1
  98. else
  99. _debug token "$CLODO_TOKEN"
  100. fi
  101. }
  102. _get_root() {
  103. domain=$1
  104. i=2
  105. p=1
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. _debug h "$h"
  109. if [ -z "$h" ]; then
  110. #not valid
  111. return 1
  112. fi
  113. if ! _clodo_rest GET "dns"; then
  114. return 1
  115. fi
  116. if _contains "$response" "\"name\":\"$h\""; then
  117. _domain_id=$(printf "%s" "$response" | _egrep_o "\"id\":\"[^\"]*\",\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1 | tr -d \")
  118. if [ "$_domain_id" ]; then
  119. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  120. _domain=$h
  121. return 0
  122. fi
  123. return 1
  124. fi
  125. p=$i
  126. i=$(_math "$i" + 1)
  127. done
  128. return 1
  129. }
  130. _clodo_rest() {
  131. m=$1
  132. ep="$2"
  133. data="$3"
  134. _debug "$ep"
  135. export _H1="X-Auth-Token: $CLODO_TOKEN"
  136. export _H2="Accept: application/json"
  137. export _H3="Content-Type: application/json"
  138. if [ "$m" != "GET" ]; then
  139. _debug data "$data"
  140. response="$(_post "$data" "$CLODO_API/$ep" "" "$m")"
  141. else
  142. response="$(_get "$CLODO_API/$ep")"
  143. fi
  144. if [ "$?" != "0" ]; then
  145. _err "error $ep"
  146. return 1
  147. fi
  148. _debug2 response "$response"
  149. return 0
  150. }