| 1 | package Net::DNS::AutoDNS::Zone; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use XML::LibXML; | ||
| 7 | use Net::DNS::Zone::ParserX; | ||
| 8 | use File::Temp (); | ||
| 9 | use Carp (); | ||
| 10 | |||
| 11 | # ABSTRACT: Class to represent one zone in AutoDNS | ||
| 12 | |||
| 13 | |||
| 14 | my @attributes = qw( | ||
| 15 | name ns_action allow_transfer_from allow_transfer internal_ns | ||
| 16 | www_include | ||
| 17 | ); | ||
| 18 | |||
| 19 | |||
| 20 | sub rr { return shift->{rr} } ## no critic | ||
| 21 | |||
| 22 | |||
| 23 | sub name { return shift->{name} } ## no critic | ||
| 24 | |||
| 25 | |||
| 26 | sub ns_action { ## no critic | ||
| 27 | return @_ > 1 ? shift->{ns_action} = pop() : shift->{ns_action}; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub allow_transfer_from { ## no critic | ||
| 32 | return @_ > 1 | ||
| 33 | ? shift->{allow_transfer_from} = pop() | ||
| 34 | : shift->{allow_transfer_from}; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub allow_transfer { ## no critic | ||
| 39 | return @_ > 1 ? shift->{allow_transfer} = pop() : shift->{allow_transfer}; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub internal_ns { ## no critic | ||
| 44 | return @_ > 1 ? shift->{internal_ns} = pop() : shift->{internal_ns}; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub www_include { ## no critic | ||
| 49 | return @_ > 1 ? shift->{www_include} = pop() : shift->{www_include}; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub new { | ||
| 54 | my ($class, $name) = @_; | ||
| 55 | my $self = bless {}, $class; | ||
| 56 | $self->{name} = $name; | ||
| 57 | $self->{ns_action} = 'complete'; | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub clone { | ||
| 64 | my ($self, $name) = @_; | ||
| 65 | my $clone = bless {%$self}, ref $self; | ||
| 66 | $clone->{name} = $name if defined $name; | ||
| 67 | |||
| 68 | # Clone RR records via string dump + parse | ||
| 69 | $clone->{rr} = [ map { Net::DNS::RR->new($_->string) } @${ $self->{rr} } ]; | ||
| 70 | return $clone; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub new_from_zonefile { | ||
| 75 | my ($class, $name, $zonefile) = @_; | ||
| 76 | |||
| 77 | my $self = $class->new($name); | ||
| 78 | $self->read_rr_from_zonefile($zonefile); | ||
| 79 | |||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub new_from_xml { | ||
| 85 | my ($class, $xml, $parser) = @_; | ||
| 86 | |||
| 87 | $parser ||= XML::LibXML->new; | ||
| 88 | my $doc = $parser->parse_string($xml); | ||
| 89 | |||
| 90 | my $self = $class->new(""); # name is updated later | ||
| 91 | |||
| 92 | $self->{$_} = join "", map { $_->textContent } $doc->findnodes("//zone/$_") | ||
| 93 | for @attributes; | ||
| 94 | |||
| 95 | my $rr = $self->{rr} = []; | ||
| 96 | |||
| 97 | for my $node ($doc->findnodes("//zone/soa")) { | ||
| 98 | my %attr; | ||
| 99 | $attr{ $_->nodeName } = $_->textContent for $node->childNodes; | ||
| 100 | my $email = $attr{email}; | ||
| 101 | my $ns = | ||
| 102 | $self->{internal_ns} | ||
| 103 | || $doc->findvalue("//zone/nserver[1]/name") | ||
| 104 | || 'intentionally.invalid'; | ||
| 105 | $email =~ s/\@/./; | ||
| 106 | push @$rr, | ||
| 107 | Net::DNS::RR->new(qq[XXORIGINXX. SOA $ns $email (] | ||
| 108 | . time | ||
| 109 | . qq[ $attr{refresh} $attr{retry} $attr{expire} $attr{ttl})]); | ||
| 110 | } | ||
| 111 | |||
| 112 | for my $node ($doc->findnodes("//zone/nserver")) { | ||
| 113 | my %attr; | ||
| 114 | $attr{ $_->nodeName } = $_->textContent for $node->childNodes; | ||
| 115 | push @$rr, | ||
| 116 | Net::DNS::RR->new( | ||
| 117 | type => 'NS', | ||
| 118 | name => 'XXORIGINXX', | ||
| 119 | ttl => $attr{ttl}, | ||
| 120 | nsdname => $attr{name}, | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | for my $node ($doc->findnodes("//zone/main")) { | ||
| 125 | my %attr; | ||
| 126 | $attr{ $_->nodeName } = $_->textContent for $node->childNodes; | ||
| 127 | push @$rr, | ||
| 128 | Net::DNS::RR->new( | ||
| 129 | type => 'A', | ||
| 130 | name => 'XXORIGINXX', | ||
| 131 | ttl => $attr{ttl}, | ||
| 132 | address => $attr{value}, | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | for my $node ($doc->findnodes("//zone/rr")) { | ||
| 137 | my %attr; | ||
| 138 | $attr{ $_->nodeName } = $_->textContent for $node->childNodes; | ||
| 139 | |||
| 140 | utf8::encode($_) for values %attr; # work around WEIRD bug in Net::DNS | ||
| 141 | # Somehow, the following fails if the name has the UTF8 flag set, even | ||
| 142 | # if there are only ASCII characters: | ||
| 143 | # Net::DNS::RR->new(type => "NAPTR", name => "naptr.XXORIGINXX", | ||
| 144 | # qw(order 10 preference 20 flags u service E2U+sip regexp !x!i | ||
| 145 | # replacement hostname) | ||
| 146 | |||
| 147 | my $type = uc $attr{type}; | ||
| 148 | my $pref = $attr{pref}; | ||
| 149 | my $name = $attr{name}; | ||
| 150 | my $value = $attr{value}; | ||
| 151 | my $ttl = $attr{ttl}; | ||
| 152 | |||
| 153 | $value =~ s/\Q$self->{name}\E\.$/XXORIGINXX./; | ||
| 154 | |||
| 155 | my @value = split " ", $value; | ||
| 156 | |||
| 157 | $name = $name ? "$name.XXORIGINXX" : "XXORIGINXX"; | ||
| 158 | |||
| 159 | if ($type eq 'TXT') { | ||
| 160 | |||
| 161 | # Special case for TXT because it won't be created from a hash | ||
| 162 | $value =~ s/([\"])/\\$1/g; | ||
| 163 | push @$rr, | ||
| 164 | Net::DNS::RR->new(qq[$name.XXORIGINXX. $ttl TXT "$value"]); | ||
| 165 | next; | ||
| 166 | } | ||
| 167 | push @$rr, | ||
| 168 | Net::DNS::RR->new( | ||
| 169 | name => $name, | ||
| 170 | ttl => $ttl, | ||
| 171 | type => $type, | ||
| 172 | $type eq 'A' ? (address => $value) | ||
| 173 | : $type eq 'AAAA' ? (address => $value) | ||
| 174 | : $type eq 'CNAME' ? (cname => $value) | ||
| 175 | : $type eq 'NS' ? (nsdname => $value) | ||
| 176 | : $type eq 'MX' ? (exchange => $value, preference => $pref) | ||
| 177 | : $type eq 'PTR' ? (ptrdname => $value) | ||
| 178 | : $type eq 'HINFO' ? (cpu => $value[0], os => $value[1]) | ||
| 179 | : $type eq 'SRV' ? ( | ||
| 180 | priority => $pref, | ||
| 181 | weight => $value[0], | ||
| 182 | port => $value[1], | ||
| 183 | target => $value[2]) | ||
| 184 | : $type eq 'NAPTR' ? ( | ||
| 185 | order => $pref, | ||
| 186 | preference => $value[0], | ||
| 187 | flags => $value[1], | ||
| 188 | service => $value[2], | ||
| 189 | regexp => $value[3], | ||
| 190 | replacement => $value[4]) | ||
| 191 | : die "Unsupported RR type, $type" | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | |||
| 195 | for my $node ($doc->findnodes("//zone/free")) { | ||
| 196 | my $free = $node->textContent; | ||
| 197 | $free =~ s/\.\Q$self->{name}\E\.(?=\s)/.XXORIGINXX./; | ||
| 198 | push @$rr, Net::DNS::RR->new($node->textContent); | ||
| 199 | } | ||
| 200 | |||
| 201 | return $self; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub read_rr_from_zonefile { | ||
| 206 | my ($self, $zonefile) = @_; | ||
| 207 | |||
| 208 | $zonefile =~ | ||
| 209 | s/((?<!\S)(?:\S+\.)?)\Q$self->{name}\E\.(?=$|\s)/${1}XXORIGINXX./mg; | ||
| 210 | |||
| 211 | my $tmp = File::Temp->new(UNLINK => 0); | ||
| 212 | print {$tmp} $zonefile; | ||
| 213 | close $tmp or die "Could not write to temporary file: $!"; | ||
| 214 | |||
| 215 | my $parser = Net::DNS::Zone::ParserX->new; | ||
| 216 | my $error = $parser->read( | ||
| 217 | $tmp->filename, { | ||
| 218 | CREATE_RR => 1, | ||
| 219 | ORIGIN => "XXORIGINXX.", | ||
| 220 | }); | ||
| 221 | |||
| 222 | unlink $tmp or 1; | ||
| 223 | Carp::croak($error) if $error; | ||
| 224 | |||
| 225 | $self->{rr} = $parser->get_array; | ||
| 226 | |||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | sub zonefile { | ||
| 232 | my ($self) = @_; | ||
| 233 | |||
| 234 | my @zonefile = map { $_->string . "\n" } @{ $self->{rr} }; | ||
| 235 | s/XXORIGINXX\./$self->{name}./g for @zonefile; | ||
| 236 | s/^(?=\s)/\@/g for @zonefile; | ||
| 237 | |||
| 238 | return join "", @zonefile; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | sub xml { | ||
| 243 | my ($self) = @_; | ||
| 244 | |||
| 245 | my $records = ""; | ||
| 246 | my %seen; | ||
| 247 | for (@{ $self->{rr} }) { | ||
| 248 | my $record = $_->autodns_xml; | ||
| 249 | |||
| 250 | # Silently discard excess records, beyond AutoDNS' artificial limits | ||
| 251 | $records .= $record | ||
| 252 | unless $record =~ /<main>/ and ++$seen{main} > 1 | ||
| 253 | or $record =~ /<nserver>/ and ++$seen{nserver} > 7; | ||
| 254 | } | ||
| 255 | |||
| 256 | my $xml = | ||
| 257 | Net::DNS::AutoDNS::_xmlify("zone", | ||
| 258 | (map { $_ => $self->{$_} } @attributes), \$records); | ||
| 259 | $xml =~ s/XXORIGINXX\.?/$self->{name}./g; | ||
| 260 | |||
| 261 | return $xml; | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | 1; | ||
| 266 | |||
| 267 | __END__ |