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.

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