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.

149 lines
3.6 KiB

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