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.

187 lines
5.0 KiB

  1. #!/usr/bin/env sh
  2. #
  3. #GCORE_Key='773$7b7adaf2a2b32bfb1b83787b4ff32a67eb178e3ada1af733e47b1411f2461f7f4fa7ed7138e2772a46124377bad7384b3bb8d87748f87b3f23db4b8bbe41b2bb'
  4. #
  5. GCORE_Api="https://api.gcorelabs.com/dns/v2"
  6. GCORE_Doc="https://apidocs.gcore.com/dns"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_gcore_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}"
  13. if [ -z "$GCORE_Key" ]; then
  14. GCORE_Key=""
  15. _err "You didn't specify a Gcore api key yet."
  16. _err "You can get yours from here $GCORE_Doc"
  17. return 1
  18. fi
  19. #save the api key to the account conf file.
  20. _saveaccountconf_mutable GCORE_Key "$GCORE_Key"
  21. _debug "First detect the zone name"
  22. if ! _get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug _zone_name "$_zone_name"
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _debug "Getting txt records"
  30. _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT"
  31. payload=""
  32. if echo "$response" | grep "record is not found" >/dev/null; then
  33. _info "Record doesn't exists"
  34. payload="{\"resource_records\":[{\"content\":[\"$txtvalue\"],\"enabled\":true}],\"ttl\":120}"
  35. elif echo "$response" | grep "$txtvalue" >/dev/null; then
  36. _info "Already exists, OK"
  37. return 0
  38. elif echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then
  39. _info "Record with mismatch txtvalue, try update it"
  40. payload=$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/"meta":{}}]}/"meta":{}},{"content":['\""$txtvalue"\"'],"enabled":true}]}/')
  41. fi
  42. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  43. # we can not use updating anymore.
  44. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  45. # _debug count "$count"
  46. # if [ "$count" = "0" ]; then
  47. _info "Adding record"
  48. if _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then
  49. if _contains "$response" "$txtvalue"; then
  50. _info "Added, OK"
  51. return 0
  52. elif _contains "$response" "rrset is already exists"; then
  53. _info "Already exists, OK"
  54. return 0
  55. else
  56. _err "Add txt record error."
  57. return 1
  58. fi
  59. fi
  60. _err "Add txt record error."
  61. return 1
  62. }
  63. #fulldomain txtvalue
  64. dns_gcore_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}"
  68. _debug "First detect the root zone"
  69. if ! _get_root "$fulldomain"; then
  70. _err "invalid domain"
  71. return 1
  72. fi
  73. _debug _zone_name "$_zone_name"
  74. _debug _sub_domain "$_sub_domain"
  75. _debug _domain "$_domain"
  76. _debug "Getting txt records"
  77. _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT"
  78. if echo "$response" | grep "record is not found" >/dev/null; then
  79. _info "No such txt recrod"
  80. return 0
  81. fi
  82. if ! echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then
  83. _err "Error: $response"
  84. return 1
  85. fi
  86. if ! echo "$response" | tr -d " " | grep \""$txtvalue"\" >/dev/null; then
  87. _info "No such txt recrod"
  88. return 0
  89. fi
  90. count="$(echo "$response" | grep -o "content" | wc -l)"
  91. if [ "$count" = "1" ]; then
  92. if ! _gcore_rest DELETE "zones/$_zone_name/$fulldomain/TXT"; then
  93. _err "Delete record error. $response"
  94. return 1
  95. fi
  96. return 0
  97. fi
  98. payload="$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/{"id":[0-9]\+,"content":\["'"$txtvalue"'"\],"enabled":true,"meta":{}}//' | sed 's/\[,/\[/' | sed 's/,,/,/' | sed 's/,\]/\]/')"
  99. if ! _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then
  100. _err "Delete record error. $response"
  101. fi
  102. }
  103. #################### Private functions below ##################################
  104. #_acme-challenge.sub.domain.com
  105. #returns
  106. # _sub_domain=_acme-challenge.sub or _acme-challenge
  107. # _domain=domain.com
  108. # _zone_name=domain.com or sub.domain.com
  109. _get_root() {
  110. domain=$1
  111. i=1
  112. p=1
  113. while true; do
  114. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  115. _debug h "$h"
  116. if [ -z "$h" ]; then
  117. #not valid
  118. return 1
  119. fi
  120. if ! _gcore_rest GET "zones/$h"; then
  121. return 1
  122. fi
  123. if _contains "$response" "\"name\":\"$h\""; then
  124. _zone_name=$h
  125. if [ "$_zone_name" ]; then
  126. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  127. _domain=$h
  128. return 0
  129. fi
  130. return 1
  131. fi
  132. p=$i
  133. i=$(_math "$i" + 1)
  134. done
  135. return 1
  136. }
  137. _gcore_rest() {
  138. m=$1
  139. ep="$2"
  140. data="$3"
  141. _debug "$ep"
  142. key_trimmed=$(echo "$GCORE_Key" | tr -d '"')
  143. export _H1="Content-Type: application/json"
  144. export _H2="Authorization: APIKey $key_trimmed"
  145. if [ "$m" != "GET" ]; then
  146. _debug data "$data"
  147. response="$(_post "$data" "$GCORE_Api/$ep" "" "$m")"
  148. else
  149. response="$(_get "$GCORE_Api/$ep")"
  150. fi
  151. if [ "$?" != "0" ]; then
  152. _err "error $ep"
  153. return 1
  154. fi
  155. _debug2 response "$response"
  156. return 0
  157. }