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.

191 lines
5.3 KiB

  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. return 1
  74. }
  75. # Usage fulldomain txtvalue
  76. # Remove the txt record after validation
  77. dns_servercow_rm() {
  78. fulldomain=$1
  79. txtvalue=$2
  80. _info "Using servercow"
  81. _debug fulldomain "$fulldomain"
  82. _debug txtvalue "$fulldomain"
  83. SERVERCOW_API_Username="${SERVERCOW_API_Username:-$(_readaccountconf_mutable SERVERCOW_API_Username)}"
  84. SERVERCOW_API_Password="${SERVERCOW_API_Password:-$(_readaccountconf_mutable SERVERCOW_API_Password)}"
  85. if [ -z "$SERVERCOW_API_Username" ] || [ -z "$SERVERCOW_API_Password" ]; then
  86. SERVERCOW_API_Username=""
  87. SERVERCOW_API_Password=""
  88. _err "You don't specify servercow api username and password yet."
  89. _err "Please create your username and password and try again."
  90. return 1
  91. fi
  92. _debug "First detect the root zone"
  93. if ! _get_root "$fulldomain"; then
  94. _err "invalid domain"
  95. return 1
  96. fi
  97. _debug _sub_domain "$_sub_domain"
  98. _debug _domain "$_domain"
  99. if _servercow_api DELETE "$_domain" "{\"type\":\"TXT\",\"name\":\"$fulldomain\"}"; then
  100. if printf -- "%s" "$response" | grep "ok" >/dev/null; then
  101. _info "Deleted, OK"
  102. _contains "$response" '"message":"ok"'
  103. else
  104. _err "delete txt record error."
  105. return 1
  106. fi
  107. fi
  108. }
  109. #################### Private functions below ##################################
  110. # _acme-challenge.www.domain.com
  111. # returns
  112. # _sub_domain=_acme-challenge.www
  113. # _domain=domain.com
  114. _get_root() {
  115. fulldomain=$1
  116. i=2
  117. p=1
  118. while true; do
  119. _domain=$(printf "%s" "$fulldomain" | cut -d . -f $i-100)
  120. _debug _domain "$_domain"
  121. if [ -z "$_domain" ]; then
  122. # not valid
  123. return 1
  124. fi
  125. if ! _servercow_api GET "$_domain"; then
  126. return 1
  127. fi
  128. if ! _contains "$response" '"error":"no such domain in user context"' >/dev/null; then
  129. _sub_domain=$(printf "%s" "$fulldomain" | cut -d . -f 1-$p)
  130. if [ -z "$_sub_domain" ]; then
  131. # not valid
  132. return 1
  133. fi
  134. return 0
  135. fi
  136. p=$i
  137. i=$(_math "$i" + 1)
  138. done
  139. return 1
  140. }
  141. _servercow_api() {
  142. method=$1
  143. domain=$2
  144. data="$3"
  145. export _H1="Content-Type: application/json"
  146. export _H2="X-Auth-Username: $SERVERCOW_API_Username"
  147. export _H3="X-Auth-Password: $SERVERCOW_API_Password"
  148. if [ "$method" != "GET" ]; then
  149. _debug data "$data"
  150. response="$(_post "$data" "$SERVERCOW_API/$domain" "" "$method")"
  151. else
  152. response="$(_get "$SERVERCOW_API/$domain")"
  153. fi
  154. if [ "$?" != "0" ]; then
  155. _err "error $domain"
  156. return 1
  157. fi
  158. _debug2 response "$response"
  159. return 0
  160. }