dns_linode_v4.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\":\s*[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\":\s*\"$_sub_domain\".*}")"
  60. if [ "$resource" ]; then
  61. _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"id\":\s*[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. if [ -z "$LINODE_V4_API_KEY" ]; then
  84. LINODE_V4_API_KEY=""
  85. _err "You didn't specify the Linode v4 API key yet."
  86. _err "Please create your key and try again."
  87. return 1
  88. fi
  89. _saveaccountconf LINODE_V4_API_KEY "$LINODE_V4_API_KEY"
  90. }
  91. #################### Private functions below ##################################
  92. #_acme-challenge.www.domain.com
  93. #returns
  94. # _sub_domain=_acme-challenge.www
  95. # _domain=domain.com
  96. # _domain_id=12345
  97. _get_root() {
  98. domain=$1
  99. i=2
  100. p=1
  101. if _rest GET; then
  102. response="$(echo "$response" | tr -d "\n" | tr '{' "|" | sed 's/|/&{/g' | tr "|" "\n")"
  103. while true; do
  104. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  105. _debug h "$h"
  106. if [ -z "$h" ]; then
  107. #not valid
  108. return 1
  109. fi
  110. hostedzone="$(echo "$response" | _egrep_o "{.*\"domain\":\s*\"$h\".*}")"
  111. if [ "$hostedzone" ]; then
  112. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "\"id\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  113. if [ "$_domain_id" ]; then
  114. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  115. _domain=$h
  116. return 0
  117. fi
  118. return 1
  119. fi
  120. p=$i
  121. i=$(_math "$i" + 1)
  122. done
  123. fi
  124. return 1
  125. }
  126. #method method action data
  127. _rest() {
  128. mtd="$1"
  129. ep="$2"
  130. data="$3"
  131. _debug mtd "$mtd"
  132. _debug ep "$ep"
  133. export _H1="Accept: application/json"
  134. export _H2="Content-Type: application/json"
  135. export _H3="Authorization: Bearer $LINODE_V4_API_KEY"
  136. if [ "$mtd" != "GET" ]; then
  137. # both POST and DELETE.
  138. _debug data "$data"
  139. response="$(_post "$data" "$LINODE_V4_API_URL$ep" "" "$mtd")"
  140. else
  141. response="$(_get "$LINODE_V4_API_URL$ep$data")"
  142. fi
  143. if [ "$?" != "0" ]; then
  144. _err "error $ep"
  145. return 1
  146. fi
  147. _debug2 response "$response"
  148. return 0
  149. }