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.

183 lines
4.5 KiB

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