dns_aws.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  16. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  17. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  18. AWS_ACCESS_KEY_ID=""
  19. AWS_SECRET_ACCESS_KEY=""
  20. _err "You don't specify aws route53 api key id and and api key secret yet."
  21. _err "Please create your key and try again. see $(__green $AWS_WIKI)"
  22. return 1
  23. fi
  24. #save for future use
  25. _saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  26. _saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  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. _info "Geting existing records for $fulldomain"
  36. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  37. return 1
  38. fi
  39. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  40. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  41. _debug "_resource_record" "$_resource_record"
  42. else
  43. _debug "single new add"
  44. fi
  45. if [ "$_resource_record" ] && _contains "$response" "$txtvalue"; then
  46. _info "The txt record already exists, skip"
  47. return 0
  48. fi
  49. _debug "Adding records"
  50. _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>$_resource_record<ResourceRecord><Value>\"$txtvalue\"</Value></ResourceRecord></ResourceRecords></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  51. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  52. _info "txt record updated success."
  53. return 0
  54. fi
  55. return 1
  56. }
  57. #fulldomain txtvalue
  58. dns_aws_rm() {
  59. fulldomain=$1
  60. txtvalue=$2
  61. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  62. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  63. _debug "First detect the root zone"
  64. if ! _get_root "$fulldomain"; then
  65. _err "invalid domain"
  66. return 1
  67. fi
  68. _debug _domain_id "$_domain_id"
  69. _debug _sub_domain "$_sub_domain"
  70. _debug _domain "$_domain"
  71. _info "Geting existing records for $fulldomain"
  72. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  73. return 1
  74. fi
  75. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  76. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  77. _debug "_resource_record" "$_resource_record"
  78. else
  79. _debug "no records exists, skip"
  80. return 0
  81. fi
  82. _aws_tmpl_xml="<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"><ChangeBatch><Changes><Change><Action>DELETE</Action><ResourceRecordSet><ResourceRecords>$_resource_record</ResourceRecords><Name>$fulldomain.</Name><Type>TXT</Type><TTL>300</TTL></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  83. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  84. _info "txt record deleted success."
  85. return 0
  86. fi
  87. return 1
  88. }
  89. #################### Private functions below ##################################
  90. _get_root() {
  91. domain=$1
  92. i=2
  93. p=1
  94. if aws_rest GET "2013-04-01/hostedzone"; then
  95. while true; do
  96. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  97. _debug2 "Checking domain: $h"
  98. if [ -z "$h" ]; then
  99. if _contains "$response" "<IsTruncated>true</IsTruncated>" && _contains "$response" "<NextMarker>"; then
  100. _debug "IsTruncated"
  101. _nextMarker="$(echo "$response" | _egrep_o "<NextMarker>.*</NextMarker>" | cut -d '>' -f 2 | cut -d '<' -f 1)"
  102. _debug "NextMarker" "$_nextMarker"
  103. if aws_rest GET "2013-04-01/hostedzone" "marker=$_nextMarker"; then
  104. _debug "Truncated request OK"
  105. i=2
  106. p=1
  107. continue
  108. else
  109. _err "Truncated request error."
  110. fi
  111. fi
  112. #not valid
  113. _err "Invalid domain"
  114. return 1
  115. fi
  116. if _contains "$response" "<Name>$h.</Name>"; then
  117. hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$h.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
  118. _debug hostedzone "$hostedzone"
  119. if [ "$hostedzone" ]; then
  120. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  121. if [ "$_domain_id" ]; then
  122. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  123. _domain=$h
  124. return 0
  125. fi
  126. _err "Can not find domain id: $h"
  127. return 1
  128. fi
  129. fi
  130. p=$i
  131. i=$(_math "$i" + 1)
  132. done
  133. fi
  134. return 1
  135. }
  136. #method uri qstr data
  137. aws_rest() {
  138. mtd="$1"
  139. ep="$2"
  140. qsr="$3"
  141. data="$4"
  142. _debug mtd "$mtd"
  143. _debug ep "$ep"
  144. _debug qsr "$qsr"
  145. _debug data "$data"
  146. CanonicalURI="/$ep"
  147. _debug2 CanonicalURI "$CanonicalURI"
  148. CanonicalQueryString="$qsr"
  149. _debug2 CanonicalQueryString "$CanonicalQueryString"
  150. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  151. _debug2 RequestDate "$RequestDate"
  152. #RequestDate="20161120T141056Z" ##############
  153. export _H1="x-amz-date: $RequestDate"
  154. aws_host="$AWS_HOST"
  155. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  156. SignedHeaders="host;x-amz-date"
  157. if [ -n "$AWS_SESSION_TOKEN" ]; then
  158. export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
  159. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  160. SignedHeaders="${SignedHeaders};x-amz-security-token"
  161. fi
  162. _debug2 CanonicalHeaders "$CanonicalHeaders"
  163. _debug2 SignedHeaders "$SignedHeaders"
  164. RequestPayload="$data"
  165. _debug2 RequestPayload "$RequestPayload"
  166. Hash="sha256"
  167. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  168. _debug2 CanonicalRequest "$CanonicalRequest"
  169. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  170. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  171. Algorithm="AWS4-HMAC-SHA256"
  172. _debug2 Algorithm "$Algorithm"
  173. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  174. _debug2 RequestDateOnly "$RequestDateOnly"
  175. Region="us-east-1"
  176. Service="route53"
  177. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  178. _debug2 CredentialScope "$CredentialScope"
  179. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  180. _debug2 StringToSign "$StringToSign"
  181. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  182. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  183. _secure_debug2 kSecret "$kSecret"
  184. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  185. _secure_debug2 kSecretH "$kSecretH"
  186. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  187. _debug2 kDateH "$kDateH"
  188. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  189. _debug2 kRegionH "$kRegionH"
  190. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  191. _debug2 kServiceH "$kServiceH"
  192. kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
  193. _debug2 kSigningH "$kSigningH"
  194. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  195. _debug2 signature "$signature"
  196. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  197. _debug2 Authorization "$Authorization"
  198. _H2="Authorization: $Authorization"
  199. _debug _H2 "$_H2"
  200. url="$AWS_URL/$ep"
  201. if [ "$qsr" ]; then
  202. url="$AWS_URL/$ep?$qsr"
  203. fi
  204. if [ "$mtd" = "GET" ]; then
  205. response="$(_get "$url")"
  206. else
  207. response="$(_post "$data" "$url")"
  208. fi
  209. _ret="$?"
  210. _debug2 response "$response"
  211. if [ "$_ret" = "0" ]; then
  212. if _contains "$response" "<ErrorResponse"; then
  213. _err "Response error:$response"
  214. return 1
  215. fi
  216. fi
  217. return "$_ret"
  218. }