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 bash
  2. #Author: Philipp Grosswiler <philipp.grosswiler@swiss-design.net>
  3. #How to create the Linode API key:
  4. #Sign into your Linode account and go to this page: https://manager.linode.com/profile/api
  5. #Then add an API key with label ACME and copy the new key.
  6. #export LINODE_API_KEY="..."
  7. LINODE_API_URL="https://api.linode.com/?api_key=$LINODE_API_KEY&api_action="
  8. ######## Public functions #####################
  9. #Usage: dns_linode_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  10. dns_linode_add() {
  11. fulldomain="${1}"
  12. txtvalue="${2}"
  13. if ! _Linode_API; then
  14. return 1
  15. fi
  16. _info "Using Linode"
  17. _debug "Calling: dns_linode_add() '${fulldomain}' '${txtvalue}'"
  18. _debug "First detect the root zone"
  19. if ! _get_root "$fulldomain"; then
  20. _err "Domain does not exist."
  21. return 1
  22. fi
  23. _debug _domain_id "$_domain_id"
  24. _debug _sub_domain "$_sub_domain"
  25. _debug _domain "$_domain"
  26. _parameters="&DomainID=$_domain_id&Type=TXT&Name=$_sub_domain&Target=$txtvalue"
  27. if _rest GET "domain.resource.create" "$_parameters" && [ -n "$response" ]; then
  28. _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\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. _parameters="&DomainID=$_domain_id"
  56. if _rest GET "domain.resource.list" "$_parameters" && [ -n "$response" ]; then
  57. response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  58. resource="$(echo "$response" | _egrep_o "{.*\"NAME\":\s*\"$_sub_domain\".*}")"
  59. if [ "$resource" ]; then
  60. _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"RESOURCEID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  61. if [ "$_resource_id" ]; then
  62. _debug _resource_id "$_resource_id"
  63. _parameters="&DomainID=$_domain_id&ResourceID=$_resource_id"
  64. if _rest GET "domain.resource.delete" "$_parameters" && [ -n "$response" ]; then
  65. _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
  66. _debug _resource_id "$_resource_id"
  67. if [ -z "$_resource_id" ]; then
  68. _err "Error deleting the domain resource."
  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_API_KEY" ]; then
  84. LINODE_API_KEY=""
  85. _err "You didn't specify the Linode API key yet."
  86. _err "Please create your key and try again."
  87. return 1
  88. fi
  89. _saveaccountconf LINODE_API_KEY "$LINODE_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 "domain.list"; then
  102. response="$(echo "$response" | tr -d "\n" | sed 's/{/\n&/g')"
  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 "\"DOMAINID\":\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. if [ "$mtd" != "GET" ]; then
  136. # both POST and DELETE.
  137. _debug data "$data"
  138. response="$(_post "$data" "$LINODE_API_URL$ep" "" "$mtd")"
  139. else
  140. response="$(_get "$LINODE_API_URL$ep$data")"
  141. fi
  142. if [ "$?" != "0" ]; then
  143. _err "error $ep"
  144. return 1
  145. fi
  146. _debug2 response "$response"
  147. return 0
  148. }