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.

192 lines
4.9 KiB

2 months ago
2 months ago
  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 _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. domain=$1
  106. i=2
  107. p=1
  108. if _rest GET; then
  109. response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
  110. while true; do
  111. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  112. _debug h "$h"
  113. if [ -z "$h" ]; then
  114. #not valid
  115. return 1
  116. fi
  117. hostedzone="$(echo "$response" | _egrep_o "\{.*\"domain\": *\"$h\".*}")"
  118. if [ "$hostedzone" ]; then
  119. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\": *[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  120. if [ "$_domain_id" ]; then
  121. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  122. _domain=$h
  123. return 0
  124. fi
  125. return 1
  126. fi
  127. p=$i
  128. i=$(_math "$i" + 1)
  129. done
  130. fi
  131. return 1
  132. }
  133. #method method action data
  134. _rest() {
  135. mtd="$1"
  136. ep="$2"
  137. data="$3"
  138. _debug mtd "$mtd"
  139. _debug ep "$ep"
  140. export _H1="Accept: application/json"
  141. export _H2="Content-Type: application/json"
  142. export _H3="Authorization: Bearer $LINODE_V4_API_KEY"
  143. if [ "$mtd" != "GET" ]; then
  144. # both POST and DELETE.
  145. _debug data "$data"
  146. response="$(_post "$data" "$LINODE_V4_API_URL$ep" "" "$mtd")"
  147. else
  148. response="$(_get "$LINODE_V4_API_URL$ep$data")"
  149. fi
  150. if [ "$?" != "0" ]; then
  151. _err "error $ep"
  152. return 1
  153. fi
  154. _debug2 response "$response"
  155. return 0
  156. }