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.

148 lines
3.6 KiB

  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. domain=$1
  71. i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
  72. i=$(_math "$i" - 1)
  73. while true; do
  74. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  75. if [ -z "$h" ]; then
  76. return 1 #not valid domain
  77. fi
  78. #Check API if domain exists
  79. if _lsw_api "GET" "$h"; then
  80. if [ "$_code" = "200"]; then
  81. _domain="$h"
  82. return 0
  83. fi
  84. fi
  85. i=$(_math "$i" - 1)
  86. if (( $i < 1)); then
  87. return 1 #not found
  88. fi
  89. done
  90. return 1
  91. }
  92. _lsw_api() {
  93. cmd=$1
  94. domain=$2
  95. fulldomain=$3
  96. txtvalue=$4
  97. # Construct the HTTP Authorization header
  98. export _H2="Content-Type: application/json"
  99. export _H1="X-Lsw-Auth: ${LSW_Key}"
  100. if [ "$cmd" = "GET" ]; then
  101. response="$(_get "$LSW_API/$domain")"
  102. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  103. _debug "http response code $_code"
  104. _debug response "$response"
  105. return 0
  106. fi
  107. if [ "$cmd" = "POST" ]; then
  108. data="{\"name\": \"$fulldomain.\",\"type\": \"TXT\",\"content\": [\"$txtvalue\"],\"ttl\": 60}"
  109. response="$(_post "$data" "$LSW_API/$domain/resourceRecordSets" "$data" "POST")"
  110. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  111. _debug "http response code $_code"
  112. _debug response "$response"
  113. return 0
  114. fi
  115. if [ "$cmd" = "DELETE" ]; then
  116. response="$(_post "" "$LSW_API/$domain/resourceRecordSets/$fulldomain/TXT" "" "DELETE")"
  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. return 1
  123. }