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.

244 lines
7.5 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. 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. _debug2 "Checking domain: $h"
  70. if [ -z "$h" ]; then
  71. if _contains "$response" "<IsTruncated>true</IsTruncated>" && _contains "$response" "<NextMarker>"; then
  72. _debug "IsTruncated"
  73. _nextMarker="$(echo "$response" | _egrep_o "<NextMarker>.*</NextMarker>" | cut -d '>' -f 2 | cut -d '<' -f 1)"
  74. _debug "NextMarker" "$_nextMarker"
  75. if aws_rest GET "2013-04-01/hostedzone" "marker=$_nextMarker"; then
  76. _debug "Truncated request OK"
  77. i=2
  78. p=1
  79. continue
  80. else
  81. _err "Truncated request error."
  82. fi
  83. fi
  84. #not valid
  85. _err "Invalid domain"
  86. return 1
  87. fi
  88. if _contains "$response" "<Name>$h.</Name>"; then
  89. hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$h.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
  90. _debug hostedzone "$hostedzone"
  91. if [ "$hostedzone" ]; then
  92. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  93. if [ "$_domain_id" ]; then
  94. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  95. _domain=$h
  96. return 0
  97. fi
  98. _err "Can not find domain id: $h"
  99. return 1
  100. fi
  101. fi
  102. p=$i
  103. i=$(_math "$i" + 1)
  104. done
  105. fi
  106. return 1
  107. }
  108. #method uri qstr data
  109. aws_rest() {
  110. mtd="$1"
  111. ep="$2"
  112. qsr="$3"
  113. data="$4"
  114. _debug mtd "$mtd"
  115. _debug ep "$ep"
  116. _debug qsr "$qsr"
  117. _debug data "$data"
  118. CanonicalURI="/$ep"
  119. _debug2 CanonicalURI "$CanonicalURI"
  120. CanonicalQueryString="$qsr"
  121. _debug2 CanonicalQueryString "$CanonicalQueryString"
  122. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  123. _debug2 RequestDate "$RequestDate"
  124. #RequestDate="20161120T141056Z" ##############
  125. export _H1="x-amz-date: $RequestDate"
  126. aws_host="$AWS_HOST"
  127. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  128. SignedHeaders="host;x-amz-date"
  129. if [ -n "$AWS_SESSION_TOKEN" ]; then
  130. export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
  131. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  132. SignedHeaders="${SignedHeaders};x-amz-security-token"
  133. fi
  134. _debug2 CanonicalHeaders "$CanonicalHeaders"
  135. _debug2 SignedHeaders "$SignedHeaders"
  136. RequestPayload="$data"
  137. _debug2 RequestPayload "$RequestPayload"
  138. Hash="sha256"
  139. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  140. _debug2 CanonicalRequest "$CanonicalRequest"
  141. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  142. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  143. Algorithm="AWS4-HMAC-SHA256"
  144. _debug2 Algorithm "$Algorithm"
  145. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  146. _debug2 RequestDateOnly "$RequestDateOnly"
  147. Region="us-east-1"
  148. Service="route53"
  149. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  150. _debug2 CredentialScope "$CredentialScope"
  151. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  152. _debug2 StringToSign "$StringToSign"
  153. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  154. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  155. _secure_debug2 kSecret "$kSecret"
  156. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  157. _secure_debug2 kSecretH "$kSecretH"
  158. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  159. _debug2 kDateH "$kDateH"
  160. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  161. _debug2 kRegionH "$kRegionH"
  162. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  163. _debug2 kServiceH "$kServiceH"
  164. kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
  165. _debug2 kSigningH "$kSigningH"
  166. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  167. _debug2 signature "$signature"
  168. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  169. _debug2 Authorization "$Authorization"
  170. _H2="Authorization: $Authorization"
  171. _debug _H2 "$_H2"
  172. url="$AWS_URL/$ep"
  173. if [ "$qsr" ]; then
  174. url="$AWS_URL/$ep?$qsr"
  175. fi
  176. if [ "$mtd" = "GET" ]; then
  177. response="$(_get "$url")"
  178. else
  179. response="$(_post "$data" "$url")"
  180. fi
  181. _ret="$?"
  182. if [ "$_ret" = "0" ]; then
  183. if _contains "$response" "<ErrorResponse"; then
  184. _err "Response error:$response"
  185. return 1
  186. fi
  187. fi
  188. return "$_ret"
  189. }