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.

196 lines
5.2 KiB

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