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.

184 lines
4.6 KiB

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