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.

200 lines
5.6 KiB

  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_linode_v4_info='Linode.com
  4. Site: Linode.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_linode_v4
  6. Options:
  7. LINODE_V4_API_KEY API Key
  8. Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net>, Aaron W. Swenson <aaron@grandmasfridge.org>
  9. '
  10. LINODE_V4_API_URL="https://api.linode.com/v4/domains"
  11. ######## Public functions #####################
  12. #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_linode_v4_add() {
  14. fulldomain="${1}"
  15. txtvalue="${2}"
  16. if ! _Linode_API; then
  17. return 1
  18. fi
  19. _info "Using Linode"
  20. _debug "Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'"
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "Domain does not exist."
  24. return 1
  25. fi
  26. _debug _domain_id "$_domain_id"
  27. _debug _sub_domain "$_sub_domain"
  28. _debug _domain "$_domain"
  29. _payload="{
  30. \"type\": \"TXT\",
  31. \"name\": \"$_sub_domain\",
  32. \"target\": \"$txtvalue\",
  33. \"ttl_sec\": 300
  34. }"
  35. if _rest POST "/$_domain_id/records" "$_payload" && [ -n "$response" ]; then
  36. _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"id\": *[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
  37. _debug _resource_id "$_resource_id"
  38. if [ -z "$_resource_id" ]; then
  39. _err "Error adding the domain resource."
  40. return 1
  41. fi
  42. _info "Domain resource successfully added."
  43. return 0
  44. fi
  45. return 1
  46. }
  47. #Usage: dns_linode_rm _acme-challenge.www.domain.com
  48. dns_linode_v4_rm() {
  49. fulldomain="${1}"
  50. if ! _Linode_API; then
  51. return 1
  52. fi
  53. _info "Using Linode"
  54. _debug "Calling: dns_linode_rm() '${fulldomain}'"
  55. _debug "First detect the root zone"
  56. if ! _get_root "$fulldomain"; then
  57. _err "Domain does not exist."
  58. return 1
  59. fi
  60. _debug _domain_id "$_domain_id"
  61. _debug _sub_domain "$_sub_domain"
  62. _debug _domain "$_domain"
  63. if _H4="X-Filter: { \"type\": \"TXT\", \"name\": \"$_sub_domain\" }" _rest GET "/$_domain_id/records" && [ -n "$response" ]; then
  64. response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
  65. resource="$(echo "$response" | _egrep_o "\{.*\"name\": *\"$_sub_domain\".*}")"
  66. if [ "$resource" ]; then
  67. _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"id\": *[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  68. if [ "$_resource_id" ]; then
  69. _debug _resource_id "$_resource_id"
  70. if _rest DELETE "/$_domain_id/records/$_resource_id" && [ -n "$response" ]; then
  71. # On 200/OK, empty set is returned. Check for error, if any.
  72. _error_response=$(printf "%s\n" "$response" | _egrep_o "\"errors\"" | cut -d : -f 2 | tr -d " " | _head_n 1)
  73. if [ -n "$_error_response" ]; then
  74. _err "Error deleting the domain resource: $_error_response"
  75. return 1
  76. fi
  77. _info "Domain resource successfully deleted."
  78. return 0
  79. fi
  80. fi
  81. return 1
  82. fi
  83. return 0
  84. fi
  85. return 1
  86. }
  87. #################### Private functions below ##################################
  88. _Linode_API() {
  89. LINODE_V4_API_KEY="${LINODE_V4_API_KEY:-$(_readaccountconf_mutable LINODE_V4_API_KEY)}"
  90. if [ -z "$LINODE_V4_API_KEY" ]; then
  91. LINODE_V4_API_KEY=""
  92. _err "You didn't specify the Linode v4 API key yet."
  93. _err "Please create your key and try again."
  94. return 1
  95. fi
  96. _saveaccountconf_mutable LINODE_V4_API_KEY "$LINODE_V4_API_KEY"
  97. }
  98. #################### Private functions below ##################################
  99. #_acme-challenge.www.domain.com
  100. #returns
  101. # _sub_domain=_acme-challenge.www
  102. # _domain=domain.com
  103. # _domain_id=12345
  104. _get_root() {
  105. full_host_str="$1"
  106. i=2
  107. p=1
  108. while true; do
  109. # loop through the received string (e.g. _acme-challenge.sub3.sub2.sub1.domain.tld),
  110. # starting from the lowest subdomain, and check if it's a hosted domain
  111. tst_hosted_domain=$(printf "%s" "$full_host_str" | cut -d . -f "$i"-100)
  112. _debug tst_hosted_domain "$tst_hosted_domain"
  113. if [ -z "$tst_hosted_domain" ]; then
  114. #not valid
  115. _err "Couldn't get domain from string '$full_host_str'."
  116. return 1
  117. fi
  118. _debug "Querying Linode APIv4 for hosted zone: $tst_hosted_domain"
  119. if _H4="X-Filter: {\"domain\":\"$tst_hosted_domain\"}" _rest GET; then
  120. _debug "Got response from API: $response"
  121. response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
  122. hostedzone="$(echo "$response" | _egrep_o "\{.*\"domain\": *\"$tst_hosted_domain\".*}")"
  123. if [ "$hostedzone" ]; then
  124. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\": *[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  125. _debug "Found domain hosted on Linode DNS. Zone: $tst_hosted_domain, id: $_domain_id"
  126. if [ "$_domain_id" ]; then
  127. _sub_domain=$(printf "%s" "$full_host_str" | cut -d . -f 1-"$p")
  128. _domain=$tst_hosted_domain
  129. return 0
  130. fi
  131. return 1
  132. fi
  133. p=$i
  134. i=$(_math "$i" + 1)
  135. fi
  136. done
  137. return 1
  138. }
  139. #method method action data
  140. _rest() {
  141. mtd="$1"
  142. ep="$2"
  143. data="$3"
  144. _debug mtd "$mtd"
  145. _debug ep "$ep"
  146. export _H1="Accept: application/json"
  147. export _H2="Content-Type: application/json"
  148. export _H3="Authorization: Bearer $LINODE_V4_API_KEY"
  149. if [ "$mtd" != "GET" ]; then
  150. # both POST and DELETE.
  151. _debug data "$data"
  152. response="$(_post "$data" "$LINODE_V4_API_URL$ep" "" "$mtd")"
  153. else
  154. response="$(_get "$LINODE_V4_API_URL$ep$data")"
  155. fi
  156. if [ "$?" != "0" ]; then
  157. _err "error $ep"
  158. return 1
  159. fi
  160. _debug2 response "$response"
  161. return 0
  162. }