-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1989shack.Apache.R5.xml
266 lines (204 loc) · 7.3 KB
/
1989shack.Apache.R5.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/perl -w
# Copyright 2010 Amazon Technologies, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
#
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://aws.amazon.com/apache2.0
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied.
#
# See the License for the specific language governing permissions and
# limitations under the License.
=head1 bindtoroute53.pl
bindtoroute53.pl - Convert a BIND zone file to Amazon Route 53 ChangeResourceRecordSetsRequest XML
=head1 SYNOPSIS
This script converts a BIND zone file to Amazon Route 53 ChangeResourceRecordSetsRequest XML to migrate a zone to Route 53.
Known limitations: Only parses A, AAAA, CNAME, MX, NS, SOA, TXT, PTR. Does not handle special characters (octets). Net::DNS::ZoneFile 1.04 does not support specifying time values in short BIND format (eg. 3h), all times must be in seconds.
Dependencies: Net::DNS::ZoneFile and its dependencies
For help, try:
bindtoroute53.pl --help
Usage examples:
From zone file:
bindtoroute53.pl --ignore-origin-ns --ignore-soa --origin example.com < example.com.zone > create-request.xml
From dig:
dig @a.iana-servers.net. www.1989shack.com. | bindtoroute53.pl --dig --origin 1989shack.com > create-request.xml
=head1 OPTIONS
=over 8
=item B<--help>
Print a help message and exits.
=item B<--origin> [origin]
Specify the zone origin. (Required)
=item B<--action> [action]
Specify the actions (CREATE or DELETE), defaults to CREATE.
=item B<--ignore-origin-ns>
Ignore NS records for the origin in the input.
=item B<--ignore-soa>
Ignore SOA records in the input.
=item B<--comment> [comment]
Specify custom comment.
=item B<--dig>
Process dig output better. Also enables --ignore-soa and --ignore-origin-ns.
=back
=cut
use warnings;
use strict;
use Net::DNS::RR;
use Net::DNS::ZoneFile;
use Getopt::Long;
use Pod::Usage;
# Net::DNS:RR to Value conversion
my $TYPES = {
A => sub { $_[0]->address; },
AAAA => sub { $_[0]->address; },
SOA => sub { my ($r)=@_; join(" ", $r->mname.".", $r->rname.".", $r->serial,
$r->refresh, $r->retry, $r->expire, $r->minimum); },
NS => sub { $_[0]->nsdname."."; },
TXT => sub { "\"" . join("\" \"", $_[0]->char_str_list()) . "\""; },
CNAME => sub { $_[0]->cname."."; },
MX => sub { my ($r)=@_; $r->preference ." ". $r->exchange."."; },
PTR => sub { $_[0]->ptrdname."."; },
# RR types below aren't currently supported by Net::DNS::ZoneFile
SRV => sub { my ($r)=@_; join(" ", $r->priority, $r->weight, $r->port,$r->target."."); },
SPF => sub { "\"" . join("\" \"", $_[0]->char_str_list()) . "\""; },
};
my $help = 0;
my $ignoreNS = 0;
my $ignoreSOA = 0;
my $action = "CREATE";
my $origin = "";
my $dig = 0;
my $comment = "This change imports a zone file";
my $options = GetOptions(
"origin=s" => \$origin,
"ignore-origin-ns" => \$ignoreNS,
"ignore-soa" => \$ignoreSOA,
"dig" => \$dig,
"action=s" => \$action,
"comment=s" => \$comment,
"help" => \$help,
);
if ($help or !$options) {
pod2usage(1);
exit;
}
if ($origin eq "") {
print STDERR "Must specify zone origin using --origin.\n";
exit 1;
}
if($origin =~ /\.$/) {
# Remove trailing . to make origin simpler to work with
chop($origin);
}
if ($dig) {
$ignoreNS = 1;
$ignoreSOA = 1;
}
if ($action ne "CREATE" && $action ne "DELETE") {
print STDERR "Action must be CREATE or DELETE.\n";
exit 1;
}
# Convert zone file to stripped down data structure
my $zone;
eval {
my $zonefile = new Net::DNS::ZoneFile(\*STDIN, $origin);
$zone = [$zonefile->read];
};
if($@ || !$zone) {
print STDERR "Unable to parse zone file: $@\n";
exit 1;
}
my $records = {};
my $recordCount = 0;
my $totalRdataLength = 0;
foreach my $zonerr (@$zone) {
my $name = lc($zonerr->name);
my $type = $zonerr->type;
my $class = $zonerr->class;
if($class ne "IN") {
print STDERR "Ignoring '" . $zonerr->string . "', only IN records supported.\n";
next;
}
if($TYPES->{$type}) {
if($ignoreSOA && $type eq "SOA") {
print STDERR "Ignoring '" . $zonerr->string . "', --ignore-soa enabled.\n";
next;
}
if(!$name) {
# name is empty string or @ without $ORIGIN
$name = $origin;
}
if($name !~ /$origin\.?$/) {
if($name =~ /\.$/ || $dig) {
print STDERR "Ignoring '" . $zonerr->string . "', name must be part of zone.\n";
next;
}
$name .= ".$origin.";
}
if($name !~ /\.$/) {
$name .= ".";
}
if($ignoreNS && $type eq "NS" && $name =~ /^$origin\.$/) {
print STDERR "Ignoring '" . $zonerr->string . "', --ignore-origin-ns enabled.\n";
next;
}
$records->{$name} = $records->{$name} || {};
my $recordList = $records->{$name}->{$type} = $records->{$name}->{$type} || [];
foreach my $existingRecord (@$recordList) {
if($existingRecord->{TTL} != $zonerr->ttl) {
die "Can't process '".$zonerr->string.", its TTL value is "
."different from other records of the same name and type.";
}
}
my $record = {
TTL => $zonerr->ttl,
Value => $TYPES->{$type}->($zonerr),
};
$totalRdataLength += length($record->{Value});
if($totalRdataLength > 32000) {
die "The ResourceRecord data exceeds 32000 characters. Try"
. " separating into multiple batches.";
}
$recordCount++;
if($recordCount > 1000) {
die "ResourceRecords exceeds 1000. Try separating into"
. " multiple batches.";
}
push(@$recordList, $record);
} else {
print STDERR "Ignoring '" . $zonerr->string . "', invalid type $type\n";
next;
}
}
# Print ChangeResourceRecordSetsRequest XML
print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print "<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2010-10-01/\">\n";
print " <ChangeBatch>\n";
print " <Comment>$comment</Comment>\n";
print " <Changes>\n";
foreach my $name (keys %$records) {
foreach my $type (keys %{$records->{$name}}) {
my $ttl = $records->{$name}->{$type}->[0]->{"TTL"};
print " <Change>\n";
print " <Action>$action</Action>\n";
print " <ResourceRecordSet>\n";
print " <Name>$name</Name>\n";
print " <Type>$type</Type>\n";
print " <TTL>$ttl</TTL>\n";
print " <ResourceRecords>\n";
foreach my $rr (@{$records->{$name}->{$type}}) {
my $value = $rr->{"Value"};
print " <ResourceRecord>\n";
print " <Value>$value</Value>\n";
print " </ResourceRecord>\n";
}
print " </ResourceRecords>\n";
print " </ResourceRecordSet>\n";
print " </Change>\n";
}
}
print " </Changes>\n";
print " </ChangeBatch>\n";
print "</ChangeResourceRecordSetsRequest>\n";