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.

185 lines
4.5 KiB

  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/{/\
  54. &/g')"
  55. resource="$(echo "$response" | _egrep_o "{.*\"NAME\":\s*\"$_sub_domain\".*}")"
  56. if [ "$resource" ]; then
  57. _resource_id=$(printf "%s\n" "$resource" | _egrep_o "\"RESOURCEID\":\s*[0-9]+" | _head_n 1 | cut -d : -f 2 | tr -d \ )
  58. if [ "$_resource_id" ]; then
  59. _debug _resource_id "$_resource_id"
  60. _parameters="&DomainID=$_domain_id&ResourceID=$_resource_id"
  61. if _rest GET "domain.resource.delete" "$_parameters" && [ -n "$response" ]; then
  62. _resource_id=$(printf "%s\n" "$response" | _egrep_o "\"ResourceID\":\s*[0-9]+" | cut -d : -f 2 | tr -d " " | _head_n 1)
  63. _debug _resource_id "$_resource_id"
  64. if [ -z "$_resource_id" ]; then
  65. _err "Error deleting the domain resource."
  66. return 1
  67. fi
  68. _info "Domain resource successfully deleted."
  69. return 0
  70. fi
  71. fi
  72. return 1
  73. fi
  74. return 0
  75. fi
  76. return 1
  77. }
  78. #################### Private functions below ##################################
  79. _Linode_API() {
  80. if [ -z "$LINODE_API_KEY" ]; then
  81. LINODE_API_KEY=""
  82. _err "You didn't specify the Linode API key yet."
  83. _err "Please create your key and try again."
  84. return 1
  85. fi
  86. _saveaccountconf LINODE_API_KEY "$LINODE_API_KEY"
  87. }
  88. #################### Private functions below ##################################
  89. #_acme-challenge.www.domain.com
  90. #returns
  91. # _sub_domain=_acme-challenge.www
  92. # _domain=domain.com
  93. # _domain_id=12345
  94. _get_root() {
  95. domain=$1
  96. i=2
  97. p=1
  98. if _rest GET "domain.list"; then
  99. response="$(echo "$response" | tr -d "\n" | sed 's/{/\
  100. &/g')"
  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 "\"DOMAINID\":\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. if [ "$mtd" != "GET" ]; then
  134. # both POST and DELETE.
  135. _debug data "$data"
  136. response="$(_post "$data" "$LINODE_API_URL$ep" "" "$mtd")"
  137. else
  138. response="$(_get "$LINODE_API_URL$ep$data")"
  139. fi
  140. if [ "$?" != "0" ]; then
  141. _err "error $ep"
  142. return 1
  143. fi
  144. _debug2 response "$response"
  145. return 0
  146. }