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.

155 lines
3.7 KiB

5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_leaseweb_info='Leaseweb.com
  4. Site: Leaseweb.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_leaseweb
  6. Options:
  7. LSW_Key API Key
  8. Issues: github.com/acmesh-official/acme.sh/issues/2558
  9. Author: Rolph Haspers <r.haspers@global.leaseweb.com>
  10. '
  11. #See https://developer.leaseweb.com for more information.
  12. ######## Public functions #####################
  13. LSW_API="https://api.leaseweb.com/hosting/v2/domains"
  14. #Usage: dns_leaseweb_add _acme-challenge.www.domain.com
  15. dns_leaseweb_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
  19. if [ -z "$LSW_Key" ]; then
  20. LSW_Key=""
  21. _err "You don't specify Leaseweb api key yet."
  22. _err "Please create your key and try again."
  23. return 1
  24. fi
  25. #save the api key to the account conf file.
  26. _saveaccountconf_mutable LSW_Key "$LSW_Key"
  27. _debug "First detect the root zone"
  28. if ! _get_root "$fulldomain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _debug _root_domain "$_domain"
  33. _debug _domain "$fulldomain"
  34. if _lsw_api "POST" "$_domain" "$fulldomain" "$txtvalue"; then
  35. if [ "$_code" = "201" ]; then
  36. _info "Added, OK"
  37. return 0
  38. else
  39. _err "Add txt record error, invalid code. Code: $_code"
  40. return 1
  41. fi
  42. fi
  43. _err "Add txt record error."
  44. return 1
  45. }
  46. #Usage: fulldomain txtvalue
  47. #Remove the txt record after validation.
  48. dns_leaseweb_rm() {
  49. fulldomain=$1
  50. txtvalue=$2
  51. LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
  52. _debug "First detect the root zone"
  53. if ! _get_root "$fulldomain"; then
  54. _err "invalid domain"
  55. return 1
  56. fi
  57. _debug _root_domain "$_domain"
  58. _debug _domain "$fulldomain"
  59. if _lsw_api "DELETE" "$_domain" "$fulldomain" "$txtvalue"; then
  60. if [ "$_code" = "204" ]; then
  61. _info "Deleted, OK"
  62. return 0
  63. else
  64. _err "Delete txt record error."
  65. return 1
  66. fi
  67. fi
  68. _err "Delete txt record error."
  69. return 1
  70. }
  71. #################### Private functions below ##################################
  72. # _acme-challenge.www.domain.com
  73. # returns
  74. # _domain=domain.com
  75. _get_root() {
  76. rdomain=$1
  77. i="$(echo "$rdomain" | tr '.' ' ' | wc -w)"
  78. i=$(_math "$i" - 1)
  79. while true; do
  80. h=$(printf "%s" "$rdomain" | cut -d . -f "$i"-100)
  81. _debug h "$h"
  82. if [ -z "$h" ]; then
  83. return 1 #not valid domain
  84. fi
  85. #Check API if domain exists
  86. if _lsw_api "GET" "$h"; then
  87. if [ "$_code" = "200" ]; then
  88. _domain="$h"
  89. return 0
  90. fi
  91. fi
  92. i=$(_math "$i" - 1)
  93. if [ "$i" -lt 2 ]; then
  94. return 1 #not found, no need to check _acme-challenge.sub.domain in leaseweb api.
  95. fi
  96. done
  97. return 1
  98. }
  99. _lsw_api() {
  100. cmd=$1
  101. d=$2
  102. fd=$3
  103. tvalue=$4
  104. # Construct the HTTP Authorization header
  105. export _H2="Content-Type: application/json"
  106. export _H1="X-Lsw-Auth: ${LSW_Key}"
  107. if [ "$cmd" = "GET" ]; then
  108. response="$(_get "$LSW_API/$d")"
  109. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  110. _debug "http response code $_code"
  111. _debug response "$response"
  112. return 0
  113. fi
  114. if [ "$cmd" = "POST" ]; then
  115. data="{\"name\": \"$fd.\",\"type\": \"TXT\",\"content\": [\"$tvalue\"],\"ttl\": 60}"
  116. response="$(_post "$data" "$LSW_API/$d/resourceRecordSets" "$data" "POST")"
  117. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  118. _debug "http response code $_code"
  119. _debug response "$response"
  120. return 0
  121. fi
  122. if [ "$cmd" = "DELETE" ]; then
  123. response="$(_post "" "$LSW_API/$d/resourceRecordSets/$fd/TXT" "" "DELETE")"
  124. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  125. _debug "http response code $_code"
  126. _debug response "$response"
  127. return 0
  128. fi
  129. return 1
  130. }