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.

247 lines
7.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #AWS_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #AWS_SECRET_ACCESS_KEY="xxxxxxx"
  6. #This is the Amazon Route53 api wrapper for acme.sh
  7. AWS_HOST="route53.amazonaws.com"
  8. AWS_URL="https://$AWS_HOST"
  9. AWS_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-use-Amazon-Route53-API"
  10. ######## Public functions #####################
  11. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_aws_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  16. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  17. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  18. AWS_ACCESS_KEY_ID=""
  19. AWS_SECRET_ACCESS_KEY=""
  20. _err "You don't specify aws route53 api key id and and api key secret yet."
  21. _err "Please create you key and try again. see $(__green $AWS_WIKI)"
  22. return 1
  23. fi
  24. #save for future use
  25. _saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  26. _saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  27. _debug "First detect the root zone"
  28. if ! _get_root "$fulldomain"; then
  29. _err "invalid domain"
  30. return 1
  31. fi
  32. _debug _domain_id "$_domain_id"
  33. _debug _sub_domain "$_sub_domain"
  34. _debug _domain "$_domain"
  35. _aws_tmpl_xml="<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"><ChangeBatch><Changes><Change><Action>UPSERT</Action><ResourceRecordSet><Name>$fulldomain</Name><Type>TXT</Type><TTL>300</TTL><ResourceRecords><ResourceRecord><Value>\"$txtvalue\"</Value></ResourceRecord></ResourceRecords></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  36. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  37. _info "txt record updated success."
  38. return 0
  39. fi
  40. return 1
  41. }
  42. #fulldomain txtvalue
  43. dns_aws_rm() {
  44. fulldomain=$1
  45. txtvalue=$2
  46. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  47. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  48. _debug "First detect the root zone"
  49. if ! _get_root "$fulldomain"; then
  50. _err "invalid domain"
  51. return 1
  52. fi
  53. _debug _domain_id "$_domain_id"
  54. _debug _sub_domain "$_sub_domain"
  55. _debug _domain "$_domain"
  56. _aws_tmpl_xml="<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"><ChangeBatch><Changes><Change><Action>DELETE</Action><ResourceRecordSet><ResourceRecords><ResourceRecord><Value>\"$txtvalue\"</Value></ResourceRecord></ResourceRecords><Name>$fulldomain.</Name><Type>TXT</Type><TTL>300</TTL></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  57. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  58. _info "txt record deleted success."
  59. return 0
  60. fi
  61. return 1
  62. }
  63. #################### Private functions below ##################################
  64. _get_root() {
  65. domain=$1
  66. i=2
  67. p=1
  68. if aws_rest GET "2013-04-01/hostedzone"; then
  69. _debug "response" "$response"
  70. while true; do
  71. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  72. _debug2 "Checking domain: $h"
  73. if [ -z "$h" ]; then
  74. if _contains "$response" "<IsTruncated>true</IsTruncated>" && _contains "$response" "<NextMarker>"; then
  75. _debug "IsTruncated"
  76. _nextMarker="$(echo "$response" | _egrep_o "<NextMarker>.*</NextMarker>" | cut -d '>' -f 2 | cut -d '<' -f 1)"
  77. _debug "NextMarker" "$_nextMarker"
  78. if aws_rest GET "2013-04-01/hostedzone" "marker=$_nextMarker"; then
  79. _debug "Truncated request OK"
  80. i=2
  81. p=1
  82. continue
  83. else
  84. _err "Truncated request error."
  85. fi
  86. fi
  87. #not valid
  88. _err "Invalid domain"
  89. return 1
  90. fi
  91. if _contains "$response" "<Name>$h.</Name>"; then
  92. hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$h.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
  93. _debug hostedzone "$hostedzone"
  94. if [ "$hostedzone" ]; then
  95. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  96. if [ "$_domain_id" ]; then
  97. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  98. _domain=$h
  99. return 0
  100. fi
  101. _err "Can not find domain id: $h"
  102. return 1
  103. fi
  104. fi
  105. p=$i
  106. i=$(_math "$i" + 1)
  107. done
  108. fi
  109. return 1
  110. }
  111. #method uri qstr data
  112. aws_rest() {
  113. mtd="$1"
  114. ep="$2"
  115. qsr="$3"
  116. data="$4"
  117. _debug mtd "$mtd"
  118. _debug ep "$ep"
  119. _debug qsr "$qsr"
  120. _debug data "$data"
  121. CanonicalURI="/$ep"
  122. _debug2 CanonicalURI "$CanonicalURI"
  123. CanonicalQueryString="$qsr"
  124. _debug2 CanonicalQueryString "$CanonicalQueryString"
  125. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  126. _debug2 RequestDate "$RequestDate"
  127. #RequestDate="20161120T141056Z" ##############
  128. export _H1="x-amz-date: $RequestDate"
  129. aws_host="$AWS_HOST"
  130. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  131. SignedHeaders="host;x-amz-date"
  132. if [ -n "$AWS_SESSION_TOKEN" ]; then
  133. export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
  134. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  135. SignedHeaders="${SignedHeaders};x-amz-security-token"
  136. fi
  137. _debug2 CanonicalHeaders "$CanonicalHeaders"
  138. _debug2 SignedHeaders "$SignedHeaders"
  139. RequestPayload="$data"
  140. _debug2 RequestPayload "$RequestPayload"
  141. Hash="sha256"
  142. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  143. _debug2 CanonicalRequest "$CanonicalRequest"
  144. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  145. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  146. Algorithm="AWS4-HMAC-SHA256"
  147. _debug2 Algorithm "$Algorithm"
  148. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  149. _debug2 RequestDateOnly "$RequestDateOnly"
  150. Region="us-east-1"
  151. Service="route53"
  152. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  153. _debug2 CredentialScope "$CredentialScope"
  154. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  155. _debug2 StringToSign "$StringToSign"
  156. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  157. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  158. _secure_debug2 kSecret "$kSecret"
  159. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  160. _secure_debug2 kSecretH "$kSecretH"
  161. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  162. _debug2 kDateH "$kDateH"
  163. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  164. _debug2 kRegionH "$kRegionH"
  165. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  166. _debug2 kServiceH "$kServiceH"
  167. kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
  168. _debug2 kSigningH "$kSigningH"
  169. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  170. _debug2 signature "$signature"
  171. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  172. _debug2 Authorization "$Authorization"
  173. _H2="Authorization: $Authorization"
  174. _debug _H2 "$_H2"
  175. url="$AWS_URL/$ep"
  176. if [ "$qsr" ]; then
  177. url="$AWS_URL/$ep?$qsr"
  178. fi
  179. if [ "$mtd" = "GET" ]; then
  180. response="$(_get "$url")"
  181. else
  182. response="$(_post "$data" "$url")"
  183. fi
  184. _ret="$?"
  185. if [ "$_ret" = "0" ]; then
  186. if _contains "$response" "<ErrorResponse"; then
  187. _err "Response error:$response"
  188. return 1
  189. fi
  190. fi
  191. return "$_ret"
  192. }