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.

97 lines
3.3 KiB

  1. #!/bin/sh
  2. #.Distributed under the terms of the GNU General Public License (GPL) version 2.0
  3. #.based on Yuval Adam's route53.sh found at https://github.com/yuvadm/route53-ddns/blob/master/route53.sh
  4. #.2017 Max Berger <max at berger dot name>
  5. [ -z "$CURL_SSL" ] && write_log 14 "Amazon AWS Route53 communication require cURL with SSL support. Please install"
  6. [ -z "$username" ] && write_log 14 "Service section not configured correctly! Missing key as 'username'"
  7. [ -z "$password" ] && write_log 14 "Service section not configured correctly! Missing secret as 'password'"
  8. [ -z "$domain" ] && write_log 14 "Service section not configured correctly! Missing zone id as 'domain'"
  9. set -euo pipefail
  10. IFS=$'\n\t'
  11. ENDPOINT="route53.amazonaws.com"
  12. RECORD_TTL=300
  13. RECORD_NAME="$lookup_host".
  14. [ $use_ipv6 -eq 0 ] && RECORD_TYPE="A"
  15. [ $use_ipv6 -eq 1 ] && RECORD_TYPE="AAAA"
  16. RECORD_VALUE="$LOCAL_IP"
  17. HOSTED_ZONE_ID="$domain"
  18. API_PATH="/2013-04-01/hostedzone/${HOSTED_ZONE_ID}/rrset/"
  19. AWS_ACCESS_KEY_ID="$username"
  20. AWS_SECRET_ACCESS_KEY="$password"
  21. AWS_REGION='us-east-1'
  22. AWS_SERVICE='route53'
  23. hash() {
  24. msg=$1
  25. echo -en "$msg" | openssl dgst -sha256 | sed 's/^.* //'
  26. }
  27. sign_plain() {
  28. # Sign message using a plaintext key
  29. key=$1
  30. msg=$2
  31. echo -en "$msg" | openssl dgst -hex -sha256 -hmac "$key" | sed 's/^.* //'
  32. }
  33. sign() {
  34. # Sign message using a hex formatted key
  35. key=$1
  36. msg=$2
  37. echo -en "$msg" | openssl dgst -hex -sha256 -mac HMAC -macopt "hexkey:${key}" | sed 's/^.* //'
  38. }
  39. request_body="<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
  40. <ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"> \
  41. <ChangeBatch> \
  42. <Changes> \
  43. <Change> \
  44. <Action>UPSERT</Action> \
  45. <ResourceRecordSet> \
  46. <Name>${RECORD_NAME}</Name> \
  47. <Type>${RECORD_TYPE}</Type> \
  48. <TTL>${RECORD_TTL}</TTL> \
  49. <ResourceRecords> \
  50. <ResourceRecord> \
  51. <Value>${RECORD_VALUE}</Value> \
  52. </ResourceRecord> \
  53. </ResourceRecords> \
  54. </ResourceRecordSet> \
  55. </Change> \
  56. </Changes> \
  57. </ChangeBatch> \
  58. </ChangeResourceRecordSetsRequest>"
  59. fulldate=$(date --utc +%Y%m%dT%H%M%SZ)
  60. shortdate=$(date --utc +%Y%m%d)
  61. signed_headers="host;x-amz-date"
  62. request_hash=$(hash "$request_body")
  63. canonical_request="POST\n${API_PATH}\n\nhost:route53.amazonaws.com\nx-amz-date:${fulldate}\n\n${signed_headers}\n${request_hash}"
  64. date_key=$(sign_plain "AWS4${AWS_SECRET_ACCESS_KEY}" "${shortdate}")
  65. region_key=$(sign "$date_key" $AWS_REGION)
  66. service_key=$(sign "$region_key" $AWS_SERVICE)
  67. signing_key=$(sign "$service_key" aws4_request)
  68. credential="${shortdate}/${AWS_REGION}/${AWS_SERVICE}/aws4_request"
  69. sigmsg="AWS4-HMAC-SHA256\n${fulldate}\n${credential}\n$(hash "$canonical_request")"
  70. signature=$(sign "$signing_key" "$sigmsg")
  71. authorization="AWS4-HMAC-SHA256 Credential=${AWS_ACCESS_KEY_ID}/${credential}, SignedHeaders=${signed_headers}, Signature=${signature}"
  72. ANSWER=$(curl \
  73. -X "POST" \
  74. -H "Host: route53.amazonaws.com" \
  75. -H "X-Amz-Date: ${fulldate}" \
  76. -H "Authorization: ${authorization}" \
  77. -H "Content-Type: text/xml" \
  78. -d "$request_body" \
  79. "https://${ENDPOINT}${API_PATH}")
  80. write_log 7 "${ANSWER}"
  81. echo ${ANSWER} | grep Error >/dev/null && return 1
  82. echo ${ANSWER} | grep ChangeInfo >/dev/null && return 0
  83. return 2