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.

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