AutoDNS.pm untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 261 CC 15 SCAR 28.4
CC
15
Cov
0%
_xmlify
42.0
request
42.0
_escape
6.0
CRAP 17.1
1 package Net::DNS::AutoDNS;
2  
3 use strict;
4 use warnings;
5  
6 use Net::DNS;
7 use XML::LibXML;
8 use Carp ();
9 use Net::DNS::AutoDNS::Zone;
10 use LWP::Simple qw($ua);
11  
12 # ABSTRACT: Generates XML and communicates with AutoDNS
13  
14 our $VERSION = '0.1'; # VERSION
15  
16  
17 my $XMLH = qq[<?xml version="1.0" encoding="utf-8"?>\n];
18  
19 # RFC 1035 <character-string>
20 sub _escape {
21 my ($string) = @_;
22 return $string if not $string =~ /["\s]/;
23 $string =~ s/(["\\])/\\$1/g;
24 return qq["$string"];
25 }
26  
27 # Poor XML generator because real XML is so inconvenient.
28  
29 sub _xml_escape {
30 my ($string) = @_;
31 $string = "" if not defined $string;
32 $string =~ s/([<>"'&\x7F-\xFF\x00-\x1F])/sprintf "&#%d;", ord $1/ge;
33 return $string;
34 }
35  
36 sub _xmlify {
37  
38 # @contents is key/value, where key is an element name or a reference to
39 # literal XML, and value is one of:
40 # array reference ==> key/value pairs for nesting/recursion
41 # scalar reference ==> literal XML to include
42 # text ==> text to escape and include
43 my ($element, @contents) = @_;
44 my $xml = "<$element>";
45 if (@contents == 1) {
46 my $c = $contents[0];
47 return _xmlify($element, @$c) if ref($c) eq 'ARRAY';
48 $xml .= $$c if ref($c) eq 'SCALAR';
49 $xml .= _xml_escape($c) if not ref $c;
50 }
51 else {
52 while (@contents) {
53 if (ref($contents[0]) eq 'SCALAR') {
54 $xml .= ${ shift @contents };
55 }
56 else {
57 $xml .= _xmlify(splice @contents, 0, 2);
58 }
59 }
60 }
61 $xml .= "</$element>\n";
62 return $xml;
63 }
64  
65  
66 sub new {
67 my ($class, %args) = @_;
68 my $self = bless {}, $class;
69  
70 $self->{gateway} = delete $args{gateway}
71 and $self->{user} = delete $args{user}
72 and $self->{password} = delete $args{password}
73 and $self->{context} = delete $args{context}
74 and keys(%args) == 0
75 or Carp::croak(
76 "Usage: $class->new(NAMED: gateway, user, password, context)");
77  
78 $self->{xmlparser} = XML::LibXML->new;
79  
80 return $self;
81 }
82  
83 sub _request_xml {
84 my ($self, $code, @contents) = @_;
85 return $XMLH
86 . _xmlify(
87 "request",
88 auth => [
89 user => $self->{user},
90 password => $self->{password},
91 context => $self->{context},
92 ],
93 task => [
94 code => $code,
95 @contents,
96 ]);
97 }
98  
99  
100 sub request {
101 my ($self, $code, @contents) = @_;
102 my $xml = $self->_request_xml($code, @contents);
103  
104 print STDERR $xml . $/; #debug
105 my $response = $ua->post($self->{gateway}, Content => $xml);
106 if (not $response->is_success) {
107 Carp::croak("Received HTTP error from AutoDNS ("
108 . $response->status_line
109 . ")");
110 }
111  
112 $xml = $response->content;
113 print STDERR $xml . $/; #debug
114 Carp::croak("Received empty document from AutoDNS.") if not length $xml;
115  
116 my $doc = eval { $self->{xmlparser}->parse_string($xml) };
117 Carp::croak("Could not parse response from AutoDNS: $@") if $@;
118  
119 $code = join "", map { $_->textContent } $doc->findnodes('//status/code');
120  
121 if ($code =~ /^E/) {
122 my $text = join "",
123 map { $_->textContent } $doc->findnodes('//status/text');
124 Carp::croak("Received error code $code from AutoDNS ($text)");
125 }
126  
127 return $xml, $doc if wantarray; # my ($xml, $doc) = $foo->request
128 return $xml; # my $xml = $foo->request
129 }
130  
131  
132 sub get_zone_list {
133 my ($self) = @_;
134  
135 my (undef, $doc) = $self->request('0205', view => [ offset => 0 ]);
136  
137 return map { $_->textContent } $doc->findnodes('//zone/name');
138 }
139  
140  
141 sub get_zone_xml {
142 my ($self, $name) = @_;
143  
144 return $self->request('0205', zone => [ name => $name ]);
145 }
146  
147  
148 sub get_zone {
149 my ($self, $name) = @_;
150  
151 my $xml = $self->get_zone_xml($name);
152  
153 return Net::DNS::AutoDNS::Zone->new_from_xml($xml, $self->{xmlparser});
154 }
155  
156  
157 sub create_zone {
158 my ($self, $zone) = @_;
159  
160 $zone->isa('Net::DNS::AutoDNS::Zone') or Carp::croak();
161  
162 return $self->request('0201', \$zone->xml);
163 }
164  
165  
166 sub update_zone {
167 my ($self, $zone) = @_;
168  
169 $zone->isa('Net::DNS::AutoDNS::Zone') or Carp::croak();
170  
171 return $self->request('0202', \$zone->xml);
172 }
173  
174  
175 sub delete_zone {
176 my ($self, $name) = @_;
177  
178 if ($name->isa('Net::DNS::AutoDNS::Zone')) {
179 $name = $name->{name}; # XXX
180 }
181  
182 return $self->request('0203', zone => [ name => $name ]);
183 }
184  
185 # Inject methods into alien namespaces
186  
187 sub Net::DNS::RR::SOA::autodns_xml {
188 my ($self) = @_;
189 my $email = $self->rname;
190  
191 # AutoDNS wants one @. Pointless but let's comply.
192 # XXX Resulting value is not necessarily a valid addr.
193 $email =~ s/\./\@/ unless $email =~ m/@/;
194  
195 return _xmlify(
196 "soa",
197 refresh => $self->refresh,
198 retry => $self->retry,
199 expire => $self->expire,
200 ttl => $self->minimum,
201 email => $email,
202 );
203 }
204  
205 my $generic = sub {
206 my ($self) = @_;
207  
208 my $type = $self->type;
209 my $name = $self->name;
210 $name =~ s/\.?XXORIGINXX\.?$//;
211 my $ttl = $self->ttl;
212 $ttl ||= "";
213  
214 if ($name eq '' and $type eq 'A') {
215  
216 # Special case for main IP. AutoDNS only supports zero or one;
217 # external check required to ensure a second one isn't used.
218 return _xmlify(
219 "main",
220 value => $self->address,
221 ttl => $ttl,
222 );
223 }
224  
225 if ($name eq '' and $type eq 'NS') {
226  
227 # Special case for zone nameservers. AutoDNS supports 1 to 7;
228 # whatever calls this method should check for this.
229 return _xmlify(
230 "nserver",
231 name => $self->nsdname,
232 ttl => $ttl,
233 );
234 }
235  
236 my @extra;
237 push @extra, pref => $self->preference if $type eq 'MX';
238 push @extra, pref => $self->priority if $type eq 'SRV';
239 push @extra, pref => $self->order if $type eq 'NAPTR';
240  
241 my $value = (
242 $type eq 'A' ? $self->address
243 : $type eq 'AAAA' ? $self->address
244 : $type eq 'CNAME' ? $self->cname . "."
245 : $type eq 'NS' ? $self->nsdname . "."
246 : $type eq 'MX' ? $self->exchange . "."
247 : $type eq 'PTR' ? $self->ptrdname . "."
248 : $type eq 'TXT' ? $self->txtdata
249 : $type eq 'SRV'
250 ? join(" ", map { $self->$_ } qw/weight port target/) . "."
251 : $type eq 'NAPTR' ? join(
252 " ",
253 map { _escape($self->$_) }
254 qw(
255 preference flags service regexp replacement
256 ))
257 : $type eq 'HINFO' ? join(
258 " ",
259 map { _escape($self->$_) }
260 qw(
261 cpu os
262 ))
263 : die "Unknown value type for type $type"
264 );
265  
266 return _xmlify(
267 "rr",
268 type => $type,
269 name => $name,
270 ttl => $ttl,
271 value => $value,
272 @extra,
273 );
274 };
275  
276 *Net::DNS::RR::A::autodns_xml = $generic;
277 *Net::DNS::RR::CNAME::autodns_xml = $generic;
278 *Net::DNS::RR::AAAA::autodns_xml = $generic;
279 *Net::DNS::RR::NS::autodns_xml = $generic;
280 *Net::DNS::RR::MX::autodns_xml = $generic;
281 *Net::DNS::RR::PTR::autodns_xml = $generic;
282 *Net::DNS::RR::TXT::autodns_xml = $generic;
283 *Net::DNS::RR::SRV::autodns_xml = $generic;
284 *Net::DNS::RR::NAPTR::autodns_xml = $generic;
285 *Net::DNS::RR::HINFO::autodns_xml = $generic;
286  
287  
288 1;
289  
290 __END__