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.

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