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.

220 lines
5.6 KiB

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