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.

227 lines
6.7 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
  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. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  16. AWS_ACCESS_KEY_ID=""
  17. AWS_SECRET_ACCESS_KEY=""
  18. _err "You don't specify aws route53 api key id and and api key secret yet."
  19. _err "Please create you key and try again. see $(__green $AWS_WIKI)"
  20. return 1
  21. fi
  22. if [ -z "$AWS_SESSION_TOKEN" ]; then
  23. _saveaccountconf AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  24. _saveaccountconf AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  25. fi
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _domain_id "$_domain_id"
  32. _debug _sub_domain "$_sub_domain"
  33. _debug _domain "$_domain"
  34. _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>"
  35. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  36. _info "txt record updated success."
  37. return 0
  38. fi
  39. return 1
  40. }
  41. #fulldomain txtvalue
  42. dns_aws_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. _debug "First detect the root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _domain_id "$_domain_id"
  51. _debug _sub_domain "$_sub_domain"
  52. _debug _domain "$_domain"
  53. _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>"
  54. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  55. _info "txt record deleted success."
  56. return 0
  57. fi
  58. return 1
  59. }
  60. #################### Private functions below ##################################
  61. _get_root() {
  62. domain=$1
  63. i=2
  64. p=1
  65. if aws_rest GET "2013-04-01/hostedzone"; then
  66. _debug "response" "$response"
  67. while true; do
  68. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  69. if [ -z "$h" ]; then
  70. #not valid
  71. return 1
  72. fi
  73. if _contains "$response" "<Name>$h.</Name>"; then
  74. hostedzone="$(echo "$response" | sed 's/<HostedZone>/\n&/g' | _egrep_o "<HostedZone>.*?<Name>$h.<.Name>.*?<.HostedZone>")"
  75. _debug hostedzone "$hostedzone"
  76. if [ -z "$hostedzone" ]; then
  77. _err "Error, can not get hostedzone."
  78. return 1
  79. fi
  80. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  81. if [ "$_domain_id" ]; then
  82. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  83. _domain=$h
  84. return 0
  85. fi
  86. return 1
  87. fi
  88. p=$i
  89. i=$(_math "$i" + 1)
  90. done
  91. fi
  92. return 1
  93. }
  94. #method uri qstr data
  95. aws_rest() {
  96. mtd="$1"
  97. ep="$2"
  98. qsr="$3"
  99. data="$4"
  100. _debug mtd "$mtd"
  101. _debug ep "$ep"
  102. _debug qsr "$qsr"
  103. _debug data "$data"
  104. CanonicalURI="/$ep"
  105. _debug2 CanonicalURI "$CanonicalURI"
  106. CanonicalQueryString="$qsr"
  107. _debug2 CanonicalQueryString "$CanonicalQueryString"
  108. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  109. _debug2 RequestDate "$RequestDate"
  110. #RequestDate="20161120T141056Z" ##############
  111. export _H1="x-amz-date: $RequestDate"
  112. aws_host="$AWS_HOST"
  113. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  114. SignedHeaders="host;x-amz-date"
  115. if [ -n "$AWS_SESSION_TOKEN" ]; then
  116. export _H2="x-amz-security-token: $AWS_SESSION_TOKEN"
  117. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  118. SignedHeaders="${SignedHeaders};x-amz-security-token"
  119. fi
  120. _debug2 CanonicalHeaders "$CanonicalHeaders"
  121. _debug2 SignedHeaders "$SignedHeaders"
  122. RequestPayload="$data"
  123. _debug2 RequestPayload "$RequestPayload"
  124. Hash="sha256"
  125. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  126. _debug2 CanonicalRequest "$CanonicalRequest"
  127. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  128. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  129. Algorithm="AWS4-HMAC-SHA256"
  130. _debug2 Algorithm "$Algorithm"
  131. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  132. _debug2 RequestDateOnly "$RequestDateOnly"
  133. Region="us-east-1"
  134. Service="route53"
  135. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  136. _debug2 CredentialScope "$CredentialScope"
  137. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  138. _debug2 StringToSign "$StringToSign"
  139. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  140. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  141. _debug2 kSecret "$kSecret"
  142. kSecretH="$(_hex "$kSecret")"
  143. _debug2 kSecretH "$kSecretH"
  144. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  145. _debug2 kDateH "$kDateH"
  146. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  147. _debug2 kRegionH "$kRegionH"
  148. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  149. _debug2 kServiceH "$kServiceH"
  150. kSigningH="$(printf "aws4_request%s" | _hmac "$Hash" "$kServiceH" hex)"
  151. _debug2 kSigningH "$kSigningH"
  152. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  153. _debug2 signature "$signature"
  154. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  155. _debug2 Authorization "$Authorization"
  156. _H3="Authorization: $Authorization"
  157. _debug _H3 "$_H3"
  158. url="$AWS_URL/$ep"
  159. if [ "$mtd" = "GET" ]; then
  160. response="$(_get "$url")"
  161. else
  162. response="$(_post "$data" "$url")"
  163. fi
  164. _ret="$?"
  165. if [ "$_ret" = "0" ]; then
  166. if _contains "$response" "<ErrorResponse"; then
  167. _err "Response error:$response"
  168. return 1
  169. fi
  170. fi
  171. return "$_ret"
  172. }