1 |
16b16366
|
Jason McCormick
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* r53.class for managing Amazon AWS Route53 Resources
|
4 |
179377b0
|
robjarsen
|
*
|
5 |
16b16366
|
Jason McCormick
|
* This is a simplified version of r53.class for trivial "dynDNS" operations
|
6 |
|
|
* that integrates well with pfSense. Based on the original r53.class by
|
7 |
|
|
* Dan Myers at http://sourceforge.net/projects/php-r53/ .
|
8 |
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
9 |
|
|
* Copyright (c) 2011, Dan Myers.
|
10 |
|
|
* Parts copyright (c) 2008, Donovan Schonknecht.
|
11 |
|
|
* All rights reserved.
|
12 |
|
|
*
|
13 |
|
|
* Redistribution and use in source and binary forms, with or without
|
14 |
|
|
* modification, are permitted provided that the following conditions are met:
|
15 |
|
|
*
|
16 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
* this list of conditions and the following disclaimer.
|
18 |
|
|
* - Redistributions in binary form must reproduce the above copyright
|
19 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
20 |
|
|
* documentation and/or other materials provided with the distribution.
|
21 |
|
|
*
|
22 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
23 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
24 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
25 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
26 |
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
27 |
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31 |
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32 |
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
33 |
|
|
*
|
34 |
|
|
* This is a modified BSD license (the third clause has been removed).
|
35 |
|
|
* The BSD license may be found here:
|
36 |
|
|
* http://www.opensource.org/licenses/bsd-license.php
|
37 |
|
|
*
|
38 |
|
|
* Amazon Route 53 is a trademark of Amazon.com, Inc. or its affiliates.
|
39 |
|
|
*
|
40 |
|
|
* Route53 is based on Donovan Schonknecht's Amazon S3 PHP class, found here:
|
41 |
|
|
* http://undesigned.org.za/2007/10/22/amazon-s3-php-class
|
42 |
|
|
*
|
43 |
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
44 |
|
|
*
|
45 |
|
|
*/
|
46 |
|
|
|
47 |
|
|
class Route53
|
48 |
|
|
{
|
49 |
|
|
protected $__accessKey; // AWS Access key
|
50 |
|
|
protected $__secretKey; // AWS Secret key
|
51 |
179377b0
|
robjarsen
|
|
52 |
16b16366
|
Jason McCormick
|
/**
|
53 |
|
|
* Constructor
|
54 |
|
|
*
|
55 |
|
|
* @param string $accessKey Access key
|
56 |
|
|
* @param string $secretKey Secret key
|
57 |
|
|
* @return void
|
58 |
|
|
*/
|
59 |
|
|
public function __construct($accessKey = null, $secretKey = null) {
|
60 |
|
|
if ($accessKey !== null && $secretKey !== null) {
|
61 |
|
|
$this->setAuth($accessKey, $secretKey);
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
179377b0
|
robjarsen
|
|
65 |
16b16366
|
Jason McCormick
|
/**
|
66 |
|
|
* Set AWS access key and secret key
|
67 |
|
|
*
|
68 |
|
|
* @param string $accessKey Access key
|
69 |
|
|
* @param string $secretKey Secret key
|
70 |
|
|
* @return void
|
71 |
|
|
*/
|
72 |
|
|
public function setAuth($accessKey, $secretKey) {
|
73 |
|
|
$this->__accessKey = $accessKey;
|
74 |
|
|
$this->__secretKey = $secretKey;
|
75 |
|
|
}
|
76 |
179377b0
|
robjarsen
|
|
77 |
16b16366
|
Jason McCormick
|
/**
|
78 |
|
|
* Return XML document for POST
|
79 |
|
|
*
|
80 |
|
|
* @param string $fqdn FQDN to set/update
|
81 |
|
|
* @param string $ip IP to set for the FQDN
|
82 |
|
|
* @param string $ttl TTL for the record
|
83 |
|
|
* @return string XML document
|
84 |
|
|
*/
|
85 |
|
|
public function getRequestBody($fqdn, $ip, $ttl){
|
86 |
ac5ee07e
|
Jason D. McCormick
|
$xmlreq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
87 |
16b16366
|
Jason McCormick
|
$xmlreq .= "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">";
|
88 |
|
|
$xmlreq .= "<ChangeBatch><Changes><Change>";
|
89 |
|
|
$xmlreq .= "<Action>UPSERT</Action>";
|
90 |
|
|
$xmlreq .= "<ResourceRecordSet>";
|
91 |
|
|
$xmlreq .= sprintf("<Name>%s</Name>", $fqdn);
|
92 |
|
|
$xmlreq .= "<Type>A</Type>";
|
93 |
|
|
$xmlreq .= sprintf("<TTL>%d</TTL>", $ttl);
|
94 |
|
|
$xmlreq .= sprintf("<ResourceRecords><ResourceRecord><Value>%s</Value></ResourceRecord></ResourceRecords>", $ip);
|
95 |
|
|
$xmlreq .= "</ResourceRecordSet>";
|
96 |
|
|
$xmlreq .= "</Change></Changes></ChangeBatch>";
|
97 |
|
|
$xmlreq .= "</ChangeResourceRecordSetsRequest>";
|
98 |
179377b0
|
robjarsen
|
|
99 |
49b8fbdd
|
Matthew Fine
|
return $xmlreq;
|
100 |
|
|
}
|
101 |
179377b0
|
robjarsen
|
|
102 |
49b8fbdd
|
Matthew Fine
|
public function getRequestBodyV6($fqdn, $ip, $ttl){
|
103 |
|
|
$xmlreq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
104 |
|
|
$xmlreq .= "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">";
|
105 |
|
|
$xmlreq .= "<ChangeBatch><Changes><Change>";
|
106 |
|
|
$xmlreq .= "<Action>UPSERT</Action>";
|
107 |
|
|
$xmlreq .= "<ResourceRecordSet>";
|
108 |
|
|
$xmlreq .= sprintf("<Name>%s</Name>", $fqdn);
|
109 |
|
|
$xmlreq .= "<Type>AAAA</Type>";
|
110 |
|
|
$xmlreq .= sprintf("<TTL>%d</TTL>", $ttl);
|
111 |
|
|
$xmlreq .= sprintf("<ResourceRecords><ResourceRecord><Value>%s</Value></ResourceRecord></ResourceRecords>", $ip);
|
112 |
|
|
$xmlreq .= "</ResourceRecordSet>";
|
113 |
|
|
$xmlreq .= "</Change></Changes></ChangeBatch>";
|
114 |
|
|
$xmlreq .= "</ChangeResourceRecordSetsRequest>";
|
115 |
179377b0
|
robjarsen
|
|
116 |
16b16366
|
Jason McCormick
|
return $xmlreq;
|
117 |
|
|
}
|
118 |
179377b0
|
robjarsen
|
|
119 |
16b16366
|
Jason McCormick
|
/**
|
120 |
|
|
* Return API URL
|
121 |
|
|
*
|
122 |
cb5961d1
|
Jason D. McCormick
|
* @param string $zoneid Amazon Zone ID
|
123 |
16b16366
|
Jason McCormick
|
* @return string URL
|
124 |
|
|
*/
|
125 |
|
|
public function getApiUrl($zoneid){
|
126 |
|
|
return sprintf("https://route53.amazonaws.com/2013-04-01/hostedzone/%s/rrset", $zoneid);
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
/**
|
130 |
179377b0
|
robjarsen
|
* Return HTTP post headers
|
131 |
|
|
*
|
132 |
cb5961d1
|
Jason D. McCormick
|
* @param string zoneId Amazon Zone
|
133 |
|
|
* @param string regionId Amazon Region Code (e.g. us-east-1)
|
134 |
|
|
* @param string requestBodySHA256 SHA256 hash of the request body
|
135 |
16b16366
|
Jason McCormick
|
* @return Array headers
|
136 |
|
|
*/
|
137 |
cb5961d1
|
Jason D. McCormick
|
public function getHttpPostHeaders($zoneId, $regionId, $requestBodySHA256){
|
138 |
|
|
|
139 |
|
|
$canonical_uri = sprintf("/2013-04-01/hostedzone/%s/rrset", $zoneId);
|
140 |
|
|
$amz_date = sprintf("%sT%sZ", gmdate('Ymd'), gmdate('His'));
|
141 |
|
|
$date_stamp = gmdate('Ymd');
|
142 |
|
|
|
143 |
ac5ee07e
|
Jason D. McCormick
|
$canonical_headers = sprintf("content-type:%s\nhost:%s\nx-amz-date:%s\n",
|
144 |
cb5961d1
|
Jason D. McCormick
|
"text/xml", "route53.amazonaws.com", $amz_date);
|
145 |
|
|
|
146 |
|
|
$signed_headers = "content-type;host;x-amz-date";
|
147 |
|
|
|
148 |
179377b0
|
robjarsen
|
$canonical_request = sprintf("%s\n%s\n\n%s\n%s\n%s",
|
149 |
cb5961d1
|
Jason D. McCormick
|
"POST", $canonical_uri, $canonical_headers, $signed_headers, $requestBodySHA256);
|
150 |
|
|
$algorithm = "AWS4-HMAC-SHA256";
|
151 |
ac5ee07e
|
Jason D. McCormick
|
$credential_scope = sprintf("%s/%s/%s/%s", $date_stamp, $regionId, "route53", "aws4_request");
|
152 |
179377b0
|
robjarsen
|
$string_to_sign = sprintf("%s\n%s\n%s\n%s",
|
153 |
cb5961d1
|
Jason D. McCormick
|
$algorithm, $amz_date, $credential_scope, hash("sha256", $canonical_request));
|
154 |
|
|
|
155 |
ac5ee07e
|
Jason D. McCormick
|
$kSecret = sprintf("AWS4%s", $this->__secretKey);
|
156 |
|
|
$kDate = hash_hmac("sha256", $date_stamp, $kSecret, true);
|
157 |
|
|
$kRegion = hash_hmac("sha256", $regionId, $kDate, true);
|
158 |
|
|
$kService = hash_hmac("sha256", "route53", $kRegion, true);
|
159 |
|
|
$signing_key = hash_hmac("sha256","aws4_request", $kService, true);
|
160 |
|
|
|
161 |
|
|
$signature = bin2hex(hash_hmac("sha256", $string_to_sign, $signing_key, true));
|
162 |
cb5961d1
|
Jason D. McCormick
|
|
163 |
ac5ee07e
|
Jason D. McCormick
|
$authorization_header = sprintf("%s Credential=%s/%s, SignedHeaders=%s, Signature=%s",
|
164 |
cb5961d1
|
Jason D. McCormick
|
$algorithm, $this->__accessKey, $credential_scope, $signed_headers, $signature);
|
165 |
|
|
|
166 |
|
|
$httphead[] = "Content-Type: text/xml";
|
167 |
|
|
$httphead[] = sprintf("X-Amz-Date: %s", $amz_date);
|
168 |
|
|
$httphead[] = sprintf("Authorization: %s", $authorization_header);
|
169 |
16b16366
|
Jason McCormick
|
return $httphead;
|
170 |
|
|
}
|
171 |
|
|
}
|