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.

190 lines
5.3 KiB

1 month ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_servercow_info='ServerCow.de
  4. Site: ServerCow.de
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_servercow
  6. Options:
  7. SERVERCOW_API_Username Username
  8. SERVERCOW_API_Password Password
  9. Issues: github.com/jhartlep/servercow-dns-api/issues
  10. Author: Jens Hartlep
  11. '
  12. SERVERCOW_API="https://api.servercow.de/dns/v1/domains"
  13. # Usage dns_servercow_add _acme-challenge.www.domain.com "abcdefghijklmnopqrstuvwxyz"
  14. dns_servercow_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. _info "Using servercow"
  18. _debug fulldomain "$fulldomain"
  19. _debug txtvalue "$txtvalue"
  20. SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
  21. SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
  22. if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
  23. SERVERCOW_API_Username=""
  24. SERVERCOW_API_Password=""
  25. _err "You don't specify servercow api username and password yet."
  26. _err "Please create your username and password and try again."
  27. return 1
  28. fi
  29. # save the credentials to the account conf file
  30. _saveaccountconf_mutable SERVERCOW_API_Username "$SERVERCOW_API_Username"
  31. _saveaccountconf_mutable SERVERCOW_API_Password "$SERVERCOW_API_Password"
  32. _debug "First detect the root zone"
  33. if ! _get_root "$fulldomain"; then
  34. _err "invalid domain"
  35. return 1
  36. fi
  37. _debug _sub_domain "$_sub_domain"
  38. _debug _domain "$_domain"
  39. # check whether a txt record already exists for the subdomain
  40. if printf -- "%s" "$response" | grep "{\"name\":\"$_sub_domain\",\"ttl\":20,\"type\":\"TXT\"" >/dev/null; then
  41. _info "A txt record with the same name already exists."
  42. # trim the string on the left
  43. txtvalue_old=${response#*{\"name\":\""$_sub_domain"\",\"ttl\":20,\"type\":\"TXT\",\"content\":\"}
  44. # trim the string on the right
  45. txtvalue_old=${txtvalue_old%%\"*}
  46. _debug txtvalue_old "$txtvalue_old"
  47. _info "Add the new txtvalue to the existing txt record."
  48. if _servercow_api POST "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":[\"$txtvalue\",\"$txtvalue_old\"],\"ttl\":20}"; then
  49. if printf -- "%s" "$response" | grep "ok" >/dev/null; then
  50. _info "Added additional txtvalue, OK"
  51. return 0
  52. else
  53. _err "add txt record error."
  54. return 1
  55. fi
  56. fi
  57. _err "add txt record error."
  58. return 1
  59. else
  60. _info "There is no txt record with the name yet."
  61. if _servercow_api POST "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":20}"; then
  62. if printf -- "%s" "$response" | grep "ok" >/dev/null; then
  63. _info "Added, OK"
  64. return 0
  65. else
  66. _err "add txt record error."
  67. return 1
  68. fi
  69. fi
  70. _err "add txt record error."
  71. return 1
  72. fi
  73. }
  74. # Usage fulldomain txtvalue
  75. # Remove the txt record after validation
  76. dns_servercow_rm() {
  77. fulldomain=$1
  78. txtvalue=$2
  79. _info "Using servercow"
  80. _debug fulldomain "$fulldomain"
  81. _debug txtvalue "$fulldomain"
  82. SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
  83. SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
  84. if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
  85. SERVERCOW_API_Username=""
  86. SERVERCOW_API_Password=""
  87. _err "You don't specify servercow api username and password yet."
  88. _err "Please create your username and password and try again."
  89. return 1
  90. fi
  91. _debug "First detect the root zone"
  92. if ! _get_root "$fulldomain"; then
  93. _err "invalid domain"
  94. return 1
  95. fi
  96. _debug _sub_domain "$_sub_domain"
  97. _debug _domain "$_domain"
  98. if _servercow_api DELETE "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\"}"; then
  99. if printf -- "%s" "$response" | grep "ok" >/dev/null; then
  100. _info "Deleted, OK"
  101. _contains "$response" '"message":"ok"'
  102. else
  103. _err "delete txt record error."
  104. return 1
  105. fi
  106. fi
  107. }
  108. #################### Private functions below ##################################
  109. # _acme-challenge.www.domain.com
  110. # returns
  111. # _sub_domain=_acme-challenge.www
  112. # _domain=domain.com
  113. _get_root() {
  114. fulldomain=$1
  115. i=2
  116. p=1
  117. while true; do
  118. _domain=$(printf "%s" "$fulldomain" | cut -d . -f "$i"-100)
  119. _debug _domain "$_domain"
  120. if [ -z "$_domain" ]; then
  121. # not valid
  122. return 1
  123. fi
  124. if ! _servercow_api GET "$_domain"; then
  125. return 1
  126. fi
  127. if ! _contains "$response" '"error":"no such domain in user context"' >/dev/null; then
  128. _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-"$p")
  129. if [ -z "$_sub_domain" ]; then
  130. # not valid
  131. return 1
  132. fi
  133. return 0
  134. fi
  135. p=$i
  136. i=$(_math "$i" + 1)
  137. done
  138. return 1
  139. }
  140. _servercow_api() {
  141. method=$1
  142. domain=$2
  143. data="$3"
  144. export _H1="Content-Type: application/json"
  145. export _H2="X-Auth-Username: $SERVERCOW_API_Username"
  146. export _H3="X-Auth-Password: $SERVERCOW_API_Password"
  147. if [ "$method" != "GET" ]; then
  148. _debug data "$data"
  149. response="$(_post "$data" "$SERVERCOW_API/$domain" "" "$method")"
  150. else
  151. response="$(_get "$SERVERCOW_API/$domain")"
  152. fi
  153. if [ "$?" != "0" ]; then
  154. _err "error $domain"
  155. return 1
  156. fi
  157. _debug2 response "$response"
  158. return 0
  159. }