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.

189 lines
4.7 KiB

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