DDNS/Update.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 333 CC 48 SCAR 52.4
CC
48
Cov
0%
_rfc4703_add_forward
506.0
_rfc4703_add_reverse
42.0
_rfc4703_rem_forward
42.0
CRAP 189.4
1 package Net::DHCP::DDNS::Update;
2 use warnings;
3 use strict;
4  
5 use Carp;
6 use Net::DNS;
7 use Net::DHCP::DDNS::Lookup;
8 use Net::DHCP::DDNS::TSIG;
9 use Net::IP;
10  
11  
12 ###############################################################################
13 # update request packet generator
14  
15 sub _newpacket {
16 my ( $api, $soa ) = @_;
17  
18 my $rv = new Net::DNS::Update $soa->name;
19  
20 my $tsig = Net::DHCP::DDNS::TSIG->get(
21 domain => $soa->name,
22 keydir => $api->ddns_key_root )
23 or return $rv;
24  
25 $rv->sign_tsig( $tsig->name, $tsig->value );
26  
27 return $rv;
28 }
29  
30  
31 ###############################################################################
32 # rfc4703 5.3 implementation - adding A/AAAA records
33  
34 sub _rfc4703_add_forward($$$$$@) {
35 my ( $api, $soa, $res, $dname, $dhcid, @rrset ) = @_;
36 my ( $has_v4, $has_v6 ) = ( 0, 0 );
37 my ( $num, $req, $rep );
38  
39 $num = grep {
40 $_->type eq 'A' and $has_v4 = 1;
41 $_->type eq 'AAAA' and $has_v6 = 1;
42 1;
43 } @rrset;
44  
45 unless ( $has_v4 or $has_v6 ) {
46 warn "DDNS: A or AAAA required to update $dname.\n";
47 return undef;
48 }
49  
50 if ( $api->strict_rfc4703 ) {
51 if ( $dhcid->type ne 'DHCID' ) {
52 warn "DDNS: DHCID RR required by RFC4703.\n";
53 return undef;
54 }
55 if ( $has_v6 and $dhcid->identifiertype != 2 ) {
56 warn "DDNS: DUID DHCID required with IPv6.\n";
57 return undef;
58 }
59 }
60  
61 # rfc4703 5.3.1
62  
63 retry_insert:
64 $req = _newpacket( $api, $soa );
65 $req->push( prereq => nxdomain( $dname ) );
66  
67 grep { $req->push( update => rr_add( $_->string ) ); } @rrset;
68  
69 $req->push( update => rr_add( $dhcid->string ) ) if $dhcid;
70  
71 unless ( $rep = $res->send( $req ) ) {
72 warn "DDNS error: ", $res->errorstring, "\n";
73 return undef;
74 }
75  
76 return 1 if $rep->header->rcode eq 'NOERROR';
77  
78 if ( $rep->header->rcode ne 'YXDOMAIN' ) {
79 warn "DDNS error: ", $res->errorstring, "\n";
80 return undef;
81 }
82  
83 # rfc4703 5.3.2
84  
85 unless ( $dhcid ) {
86 warn "DDNS error updating forward: no DHCID available\n";
87 return undef;
88 }
89  
90 $req = _newpacket( $api, $soa );
91 $req->push( prereq => yxdomain( $dname ) );
92 $req->push( prereq => yxrrset( $dhcid->string ) );
93  
94 if ( $has_v4 and $has_v6 ) {
95 unless ( $api->no_purge_fwd_other ) {
96 $req->push( update => rr_del( $dname . ' A' ) );
97 $req->push( update => rr_del( $dname . ' AAAA' ) );
98 }
99 }
100  
101 if ( $has_v4 and not $has_v6 ) {
102 unless ( $api->no_purge_fwd_other ) {
103 $req->push( update => rr_del( $dname . ' AAAA' ) );
104 }
105 unless ( $api->no_purge_fwd ) {
106 $req->push( update => rr_del( $dname . ' A' ) );
107 }
108 }
109  
110 if ( $has_v6 and not $has_v4 ) {
111 unless ( $api->no_purge_fwd_other ) {
112 $req->push( update => rr_del( $dname . ' A' ) );
113 }
114 unless ( $api->no_purge_fwd ) {
115 $req->push( update => rr_del( $dname . ' AAAA' ) );
116 }
117 }
118  
119 grep { $req->push( update => rr_add( $_->string ) ) } @rrset;
120  
121 unless ( $rep = $res->send( $req ) ) {
122 warn "DDNS error: ", $res->errorstring, "\n";
123 return undef;
124 }
125  
126 return 1 if $rep->header->rcode eq 'NOERROR';
127 return 0 if $rep->header->rcode eq 'NXRRSET';
128  
129 goto retry_insert if $rep->header->rcode eq 'NXDOMAIN';
130  
131 warn "DDNS error: ", $res->errorstring, "\n";
132  
133 return undef;
134 }
135  
136  
137 ###############################################################################
138 # rfc4703 5.4 implementation - adding PTR records
139  
140 sub _rfc4703_add_reverse($$$$$@) {
141 my ( $api, $soa, $res, $dname, $dhcid, @rrset ) = @_;
142 my ( $req, $rep, $dht );
143  
144 $req = _newpacket( $api, $soa );
145  
146 $dht = $dhcid->type if $dhcid;
147  
148 $req->push( update => rr_del( $dname . ' PTR' ) );
149 $req->push( update => rr_del( $dname . ' ' . $dht ) ) if $dhcid;
150  
151 foreach my $rr ( @rrset ) {
152 $req->push( update => rr_add( $rr->string ) );
153 }
154  
155 $req->push( update => rr_add( $dhcid->string ) ) if $dhcid;
156  
157 unless ( $rep = $res->send( $req ) ) {
158 warn "DDNS error: ", $res->errorstring, "\n";
159 return undef;
160 }
161  
162 return 1 if $rep->header->rcode eq 'NOERROR';
163  
164 warn "DDNS error: ", $res->errorstring, "\n";
165 return undef;
166 }
167  
168  
169 ###############################################################################
170 # rfc4703 5.5 implementation - removing records
171  
172 sub _rfc4703_rem_forward($$$$$@) {
173 my ( $api, $soa, $res, $dname, $dhcid, @rrset ) = @_;
174 my ( $req, $rep );
175 my $dtype = $dhcid->type;
176 my $dtext = $dhcid->rdatastr;
177  
178 $req = _newpacket( $api, $soa );
179  
180 $req->push( prereq => yxrrset( join( ' ', $dname, $dtype, $dtext ) ) );
181  
182 grep {
183 $req->push( update => rr_del( $_->string ) );
184 } @rrset;
185  
186 unless ( $rep = $res->send( $req ) ) {
187 warn "DDNS error: ", $res->errorstring, "\n";
188 return undef;
189 }
190  
191 return 0 if $rep->header->rcode eq 'NXRRSET';
192  
193 unless ( $rep->header->rcode eq 'NOERROR' ) {
194 warn "DDNS error: ", $res->errorstring, "\n";
195 return undef;
196 }
197  
198 $req = _newpacket( $api, $soa );
199  
200 $req->push( prereq => yxrrset( join( ' ', $dname, $dtype, $dtext ) ) );
201 $req->push( prereq => nxrrset( join( ' ', $dname, 'AAAA' ) ) );
202 $req->push( prereq => nxrrset( join( ' ', $dname, 'A' ) ) );
203 $req->push( update => rr_del( $dname ) );
204  
205 unless ( $rep = $res->send( $req ) ) {
206 warn "DDNS error: ", $res->errorstring, "\n";
207 return undef;
208 }
209  
210 return 1 if $rep->header->rcode eq 'NOERROR';
211  
212 warn "DDNS error: ", $res->errorstring, "\n";
213 return undef;
214 }
215  
216 sub _rfc4703_rem_reverse($$$$$$) {
217 my ( $api, $soa, $res, $dname, $dhcid, $value ) = @_;
218 my ( $req, $rep );
219  
220 my $pdname = $value->ptrdname;
221  
222 $req = _newpacket( $api, $soa );
223  
224 $req->push( prereq => yxrrset( join( ' ', $dname, 'PTR', $pdname ) ) );
225 $req->push( update => rr_del( $dname ) );
226  
227 unless ( $rep = $res->send( $req ) ) {
228 warn "DDNS error: ", $res->errorstring, "\n";
229 return undef;
230 }
231  
232 return 1 if $rep->header->rcode eq 'NOERROR';
233 return 0 if $rep->header->rcode eq 'NXRRSET';
234  
235 warn "DDNS error: ", $res->errorstring, "\n";
236 return undef;
237 }
238  
239  
240 ###############################################################################
241 ###############################################################################
242  
243 ###############################################################################
244 # common to all handlers
245  
246 sub _init_handler($$) {
247 my ( $api, $dname ) = @_;
248 my ( $soa, $res, $dhcid );
249  
250 $soa = Net::DHCP::DDNS::Lookup->find_soa( $dname )
251 or confess "could not find SOA for $dname";
252  
253 $res = Net::DNS::Resolver->new;
254 $res->nameservers( $soa->mname );
255  
256 $dhcid = $api->dhcid_rr( $dname );
257  
258 return ( $soa, $res, $dhcid );
259 }
260  
261  
262 ###############################################################################
263 # forward address handlers
264  
265 sub add_forward {
266 my ( $api, $dname, @rrset ) = @_;
267  
268 confess "called without dname" unless $dname;
269 confess "called without value" unless @rrset;
270  
271 my ( $soa, $res, $dhcid ) = _init_handler( $api, $dname );
272  
273 return 1 if _rfc4703_add_forward(
274 $api, $soa, $res, $dname, $dhcid, @rrset );
275  
276 # FAIL: domain is in use.
277 return 0 if $api->strict_rfc4703;
278  
279 # try alternate schemes or force the issue ?
280  
281 # FAIL: domain is in use.
282 return 0;
283 }
284  
285 sub rem_forward {
286 my ( $api, $dname, @rrset ) = @_;
287  
288 confess "called without dname" unless $dname;
289 confess "called without value" unless @rrset;
290  
291 my ( $soa, $res, $dhcid ) = _init_handler( $api, $dname );
292  
293 return 1 if _rfc4703_rem_forward(
294 $api, $soa, $res, $dname, $dhcid, @rrset );
295 return 0;
296 }
297  
298  
299 ###############################################################################
300 # reverse address handlers
301  
302 sub add_reverse {
303 my ( $api, $dname, $value ) = @_;
304  
305 confess "called without dname" unless $dname;
306 confess "called without value" unless $value;
307  
308 my ( $soa, $res, $dhcid ) = _init_handler( $api, $dname );
309  
310 return 1 if _rfc4703_add_reverse(
311 $api, $soa, $res, $dname, $dhcid, $value );
312 return 0;
313 }
314  
315 sub rem_reverse {
316 my ( $api, $dname, $value ) = @_;
317  
318 confess "called without dname" unless $dname;
319 confess "called without value" unless $value;
320  
321 my ( $soa, $res, $dhcid ) = _init_handler( $api, $dname );
322  
323 return 1 if _rfc4703_rem_reverse(
324 $api, $soa, $res, $dname, $dhcid, $value );
325 return 0;
326 }
327  
328  
329 ###############################################################################
330 1;