Project

General

Profile

« Previous | Next » 

Revision af41271b

Added by Jason McCormick over 8 years ago

move back to r53.class for license continuity

(cherry picked from commit 16b163661b1d1a5bcc9a24ce023f7a06c5fb420e)

View differences:

src/etc/inc/dyndns.class
678 678
					}
679 679
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
680 680
					break;
681
				case 'route53':
682
					/* http://docs.aws.amazon.com/Route53/latest/APIReference/Welcome.html */
683
					/* AWS3 Signature generation inspired by Dan Myers http://sourceforge.net/projects/php-r53/ */
684
					$reqdate = gmdate('D, d M Y H:i:s e');
685
					$httphead[] = array();
686
					$httphead[] = sprintf("Date: %s", $reqdate);
687
					/* to avoid having user to know their AWS Region, for now use V3 */
688
					$httphead[] = sprintf(
689
							"X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HMACSHA256,SignedHeaders=date,Signature=%s",
690
							$this->_dnsUser,
691
							base64_encode(hash_hmac("sha256", $reqdate, $this->_dnsPass, true))
692
					);					
693
					$apiurl = sprintf("https://route53.amazonaws.com/2013-04-01/hostedzone/%s/rrset", $this->_dnsZoneID);
694
					$xmlreq .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
695
					$xmlreq .= "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\">";
696
					$xmlreq .= "<ChangeBatch><Changes><Change>";
697
					$xmlreq .= "<Action>UPSERT</Action>";
698
					$xmlreq .= "<ResourceRecordSet>";
699
					$xmlreq .= sprintf("<Name>%s</Name>", $this->_dnsHost);
700
					$xmlreq .= "<Type>A</Type>";
701
					$xmlreq .= sprintf("<TTL>%d</TTL>", $this->_dnsTTL);
702
					$xmlreq .= sprintf("<ResourceRecords><ResourceRecord><Value>%s</Value></ResourceRecord></ResourceRecords>",
703
								$this->_dnsIP);
704
					$xmlreq .= "</ResourceRecordSet>";
705
					$xmlreq .= "</Change></Changes></ChangeBatch>";
706
					$xmlreq .= "</ChangeResourceRecordSetsRequest>";
707
					
708
					$httphead[] = "Content-Type: text/plain";
709
					$httphead[] = sprintf("Content-Length: %d", strlen($xmlreq));
710
					
681
				case 'route53':		
682
					require_once("r53.class");
683
					$r53 = new Route53($this->_dnsUser, $this->_dnsPass);
684
					$apiurl = $r53->getApiUrl($this->_dnsZoneID);
685
					$xmlreq = $r53->getRequestBody($this->_dnsHost, $this->_dnsIP, $this->_dnsTTL);
686
					$httphead = $r53->getHttpPostHeaders(strlen($xmlreq));
711 687
					curl_setopt($ch, CURLOPT_HTTPHEADER, $httphead);
712 688
					if($this->_dnsVerboseLog){
713 689
						log_error(sprintf("Sending reuquest to: %s", $apiurl));
714 690
						foreach($httphead as $hv){
715 691
							log_error(sprintf("Header: %s", $hv));
716 692
						}
717
						log_error(sprintf("XMLPOST:\n%s\n\n", $xmlreq));
693
						log_error(sprintf("XMLPOST: %s", $xmlreq));
718 694
					}
719 695
					curl_setopt($ch, CURLOPT_URL, $apiurl);
720 696
					curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
src/etc/inc/r53.class
1
<?php
2
/**
3
* r53.class for managing Amazon AWS Route53 Resources
4
* 
5
* 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
	
52
	/**
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
	
65
	/**
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
	
77
	/**
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
		$xmlreq .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
87
		$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
			
99
		return $xmlreq;
100
	}
101
	
102
	/**
103
	* Return API URL
104
	*
105
	* @param string $zoneid Amazone Zone ID
106
	* @return string URL
107
	*/
108
	public function getApiUrl($zoneid){
109
		return sprintf("https://route53.amazonaws.com/2013-04-01/hostedzone/%s/rrset", $zoneid);
110
	}
111

  
112
	/**
113
	* Return HTTP post headers 
114
	* 
115
	* @param int $bodylen length of the POST bost body
116
	* @return Array headers
117
	*/
118
	public function getHttpPostHeaders($bodylen){
119
		$reqdate = gmdate('D, d M Y H:i:s e');
120
		$httphead[] = array();
121
		$httphead[] = sprintf("Date: %s", $reqdate);
122
		$httphead[] = "Content-Type: text/plain";
123
		$httphead[] = sprintf("Content-Length: %d", $bodylen);
124
		/* to avoid having user to know their AWS Region, for now use V3 */
125
		$httphead[] = sprintf(
126
			"X-Amzn-Authorization: AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HMACSHA256,SignedHeaders=date,Signature=%s",
127
			$this->__accessKey,
128
			base64_encode(hash_hmac("sha256", $reqdate, $this->__secretKey, true))
129
		);
130
		return $httphead;
131
	}
132
}

Also available in: Unified diff