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.

129 lines
3.1 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. ######## Public functions #####################
  6. LSW_API="https://api.leaseweb.com/hosting/v2/domains/"
  7. #Usage: dns_leaseweb_add _acme-challenge.www.domain.com
  8. dns_leaseweb_add() {
  9. fulldomain=$1
  10. txtvalue=$2
  11. LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
  12. if [ -z "$LSW_Key" ]; then
  13. LSW_Key=""
  14. _err "You don't specify Leaseweb api key yet."
  15. _err "Please create your key and try again."
  16. return 1
  17. fi
  18. #save the api key to the account conf file.
  19. _saveaccountconf_mutable LSW_Key "$LSW_Key"
  20. _debug "First detect the root zone"
  21. if ! _get_root "$fulldomain"; then
  22. _err "invalid domain"
  23. return 1
  24. fi
  25. _debug _root_domain "$_domain"
  26. _debug _domain "$fulldomain"
  27. if _lsw_api "POST" "$_domain" "$fulldomain" "$txtvalue"; then
  28. if [ "$_code" = "201" ]; then
  29. _info "Added, OK"
  30. return 0
  31. else
  32. _err "Add txt record error, invalid code. Code: $_code"
  33. return 1
  34. fi
  35. fi
  36. _err "Add txt record error."
  37. return 1
  38. }
  39. #Usage: fulldomain txtvalue
  40. #Remove the txt record after validation.
  41. dns_leaseweb_rm() {
  42. fulldomain=$1
  43. txtvalue=$2
  44. LSW_Key="${LSW_Key:-$(_readaccountconf_mutable LSW_Key)}"
  45. _debug "First detect the root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _root_domain "$_domain"
  51. _debug _domain "$fulldomain"
  52. if _lsw_api "DELETE" "$_domain" "$fulldomain" "$txtvalue"; then
  53. if [ "$_code" = "204" ]; then
  54. _info "Deleted, OK"
  55. return 0
  56. else
  57. _err "Delete txt record error."
  58. return 1
  59. fi
  60. fi
  61. _err "Delete txt record error."
  62. return 1
  63. }
  64. #################### Private functions below ##################################
  65. # _acme-challenge.www.domain.com
  66. # returns
  67. # _domain=domain.com
  68. _get_root() {
  69. domain=$1
  70. i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
  71. i=$(_math "$i" - 1)
  72. while true; do
  73. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  74. if [ -z "$h" ]; then
  75. return 1
  76. fi
  77. _domain="$h"
  78. return 0
  79. done
  80. _debug "$domain not found"
  81. return 1
  82. }
  83. _lsw_api() {
  84. cmd=$1
  85. domain=$2
  86. fulldomain=$3
  87. txtvalue=$4
  88. # Construct the HTTP Authorization header
  89. export _H2="Content-Type: application/json"
  90. export _H1="X-Lsw-Auth: ${LSW_Key}"
  91. if [ "$cmd" = "POST" ]; then
  92. data="{\"name\": \"$fulldomain.\",\"type\": \"TXT\",\"content\": [\"$txtvalue\"],\"ttl\": 60}"
  93. response="$(_post "$data" "$LSW_API/$domain/resourceRecordSets" "$data" "POST")"
  94. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  95. _debug "http response code $_code"
  96. _debug response "$response"
  97. return 0
  98. fi
  99. if [ "$cmd" = "DELETE" ]; then
  100. response="$(_post "" "$LSW_API/$domain/resourceRecordSets/$fulldomain/TXT" "" "DELETE")"
  101. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  102. _debug "http response code $_code"
  103. _debug response "$response"
  104. return 0
  105. fi
  106. return 1
  107. }