Regather/Plugin/nsupdate.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 279 CC 28 SCAR 51.0
CC
28
Cov
0%
ldap_sync_add_modify
380.0
new
110.0
cf
2.0
CRAP 164.0
1 # -*- mode: cperl; eval: (follow-mode) -*-
2 #
3  
4 package App::Regather::Plugin::nsupdate;
5  
6 =head1 NAME
7  
8 nsupdate - RFC2136 complaint DNS zone update
9  
10 =cut
11  
12 =head1 DESCRIPTION
13  
14 plugin to update dynamis DNS zone
15  
16 logics is this:
17  
18 1. get target ip address from LDAP obj
19 2. get name server/s from reverse zone for network, ip belongs to
20 3. get list of zones to be updated
21 3.1. from config file
22 first ns_zone record is used for PTR
23  
24 3.2. from reverse zone TXT records
25 get TXT records from reverse zone for network, ip belongs to, and
26 a) here we assume, all related zones are served by the same name server/s
27 b) each TXT record to suply zone names, should be prefixed
28  
29 prefix format is: `PART1:PART2:` where
30 PART1 is config file value for 'service' -> 'service-name' -> 'ns_txt_pfx'
31 PART2 is index number 0-9 to prioritize zones (0 is the highest priority)
32 both parts should end with colon character
33  
34 so, zone name in TXT record value starts with offset = length(PART1)+3
35  
36 zone with priority 0 is used for PTR
37 c) reverse zone name is pushed to the end of list of zones to be updated
38  
39 4. check existance and get if exist, A and PTR records for LDAP obj
40 4.1. if not exists, then add new either record and return
41 4.2. if exists, then check them against LDAP obj data
42 4.2.1. if match, then return
43 4.2.2. if not match then delete allr records and -> 4.1.
44  
45 =cut
46  
47 use strict;
48 use warnings;
49 use diagnostics;
50  
51 use Data::Printer caller_info => 1, class => { expand => 2 }; # temporary stuff, to be removed
52  
53 use Socket;
54 use Net::DNS;
55 use Net::DNS::RR::TSIG;
56 use Net::LDAP;
57 use Net::LDAP::Constant qw( LDAP_SYNC_ADD
58 LDAP_SYNC_MODIFY
59 LDAP_SYNC_DELETE );
60  
61 use constant { UPDATE_UNKNOWN => 0,
62 UPDATE_SUCCESS => 1,
63 UPDATE_ERROR => 2 };
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 Creates an instance of the class and saves a reference to its
70 arguments for further use.
71  
72 =cut
73  
74 sub new {
75 my $class = shift;
76 local %_ = %{$_[0]};
77  
78 $_{log}->cc( pr => 'debug', fm => "%s: service %s; called for dn: %s",
79 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $_{s}, $_{obj}->dn, ] ) if $_{v} > 2;
80  
81 my $ns_txt_pfx = $_{cf}->get('service', $_{s}, 'ns_txt_pfx');
82 my $re = qr/^$ns_txt_pfx/;
83 my $ip = (split(/ /, $_{obj}->get_value('umiOvpnCfgIfconfigPush')))[0]; # add check for empty
84 my $ptr_z = sprintf("%s.in-addr.arpa",
85 join('.', splice( @{[ reverse( @{[ split(/\./, $ip) ]} ) ]},
86 1)
87 )
88 );
89  
90 my $resolver = new Net::DNS::Resolver;
91 my ( @z, @zones, $query, $zone, @rr, @servers );
92  
93 if ($_{cf}->is_set('service', $_{s}, 'ns_zone')) {
94 push @zones, $_{cf}->get('service', $_{s}, 'ns_zone');
95 } else {
96 $query = $resolver->query($ptr_z, "TXT");
97 if ($query) {
98 foreach $zone ( $query->answer ) {
99 push @z, substr($zone->txtdata,length($ns_txt_pfx)) if $zone->txtdata =~ /$re/;
100 }
101  
102 @zones = map { if ( index($_,':') > -1 )
103 { substr($_, index($_,':')); }
104 else { $_; }
105 } sort @z;
106  
107 } else {
108 $_{log}->cc( pr => 'err', fm => "%s: can't get TXT for zone: %s; %s",
109 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $ptr_z, $resolver->errorstring, ] );
110 }
111 }
112  
113 ####### to fix this
114 if ( ! scalar(@zones) ) {
115 $_{log}->cc( pr => 'err', fm => "%s: imposible to get zone/s", ls => [ sprintf("%s:%s",__FILE__,__LINE__) ] );
116 return;
117 }
118  
119 push @zones, $ptr_z;
120
121 if ($_{cf}->is_section('service', $_{s}, 'ns_server')) {
122 push @servers, $_{cf}->getnode('service', $_{s}, 'ns_server');
123 } else {
124 $query = $resolver->query($ptr_z, "SOA");
125 if ($query) {
126 @rr = $query->answer;
127 push @servers, $rr[0]->mname;
128 } else {
129 $_{log}->cc( pr => 'err', fm => "%s: can't get SOA for zone: %s; %s",
130 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $ptr_z, $resolver->errorstring, ] );
131 }
132 }
133  
134 $resolver->nameservers(
135 map {
136 if (my @addrs = gethostbyname($_)) {
137 my @ret = map { inet_ntoa($_) } @addrs[4 .. $#addrs];
138 } else { $_ }
139 } @servers
140 );
141  
142 # ?? whether to do anything on force ?? $_{s} = LDAP_SYNC_MODIFY if $_{force};
143  
144 bless {
145 cf => delete $_{cf},
146 force => delete $_{force},
147 log => delete $_{log},
148 obj => delete $_{obj},
149 service => delete $_{s},
150 st => delete $_{st},
151 v => delete $_{v},
152  
153 ip => $ip,
154 ptr_z => $ptr_z,
155 resolver => $resolver,
156 zones => \@zones,
157  
158 rest => \%_,
159 }, $class;
160  
161 }
162  
163 sub cf { shift->{cf} }
164 sub force { shift->{force} }
165 sub ip { shift->{ip} }
166 sub log { shift->{log} }
167 sub obj { shift->{obj} }
168 sub ptr_z { shift->{ptr_z} }
169 sub resolver { shift->{resolver} }
170 sub service { shift->{service} }
171 sub syncstate { shift->{st} }
172 sub v { shift->{v} }
173 sub zones { shift->{zones} }
174  
175 =head2 ldap_sync_add_modify
176  
177 performs nsupdate: add new, delete or modify existent records,
178 according LDAP sync state
179  
180 =cut
181  
182 sub ldap_sync_add_modify {
183 my $self = shift;
184  
185 my $d_nam = $self->obj->get_value($self->cf->get('service', $self->service, 'ns_attr'));
186 my ( $update, $fqdn_a, $fqdn_ptr, $query, @rr, $ptr, $a, $tmp, $reply);
187  
188 $self->log->cc( pr => 'debug', fm => "%s: object reverse zone: %s",
189 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->ptr_z ]) if $self->v > 2;
190  
191 $self->log->cc( pr => 'debug', fm => "%s: object hostname: %s",
192 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $d_nam, ] ) if $self->v > 2;
193  
194 my ( $zone, $param );
195 foreach $zone ( @{$self->zones} ) {
196 if ( $zone =~ /^.*\.in-addr\.arpa$/ ) {
197 $param->{type} = 'PTR';
198 $param->{byte4} = (split(/\./, $self->ip))[3];
199 $param->{fqdn} = $param->{byte4};
200 $param->{reverse} = sprintf("%s.%s", $param->{byte4}, $zone);
201 $param->{rr_add} = sprintf("%s %s %s %s.", $param->{reverse},
202 $self->cf->get('service', $self->service, 'ns_ttl'),
203 $param->{type},
204 sprintf("%s.%s", $d_nam, $self->zones->[0]) );
205 } else {
206 $param->{type} = 'A';
207 $param->{fqdn} = sprintf("%s.%s", $d_nam, $zone);
208 $param->{rr_add} = sprintf("%s. %s %s %s", $param->{fqdn},
209 $self->cf->get('service', $self->service, 'ns_ttl'),
210 $param->{type},
211 $self->ip);
212 }
213  
214 $update = new Net::DNS::Update($zone);
215 $self->log->cc( pr => 'debug', fm => "%s: object expected %s record: %s",
216 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $param->{type},
217 $param->{type} eq 'A' ? $param->{fqdn} : $param->{reverse}, ] );
218  
219 $query = $self->resolver->query($param->{type} eq 'A' ? $param->{fqdn} : $param->{reverse},
220 $param->{type});
221 if ($query) {
222 @rr = $query->answer;
223 $param->{rr} = $param->{type} eq 'A' ? $rr[0]->address : $rr[0]->ptrdname;
224  
225 $self->log->cc( pr => 'debug', fm => "%s: query type: %s; RR: %s",
226 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $param->{type}, $param->{rr} ] ) if $self->v > 2;
227  
228 } else {
229 $self->log->cc( pr => 'err', fm => "%s: unable to resolve %s record: %s; error: %s",
230 ls => [ sprintf("%s:%s",__FILE__,__LINE__),$param->{type},
231 $param->{type} eq 'A' ? $param->{fqdn} : $param->{reverse},
232 $self->resolver->errorstring, ] );
233 }
234  
235  
236 if ( $self->syncstate == LDAP_SYNC_ADD && ! defined $param->{rr} ) {
237  
238 $update->push( pre => nxrrset( sprintf("%s. %s",
239 $param->{type} eq 'A' ? $param->{fqdn} : $param->{reverse},
240 $param->{type}) ) );
241 $update->push( update => rr_add( $param->{rr_add} ) );
242  
243 } elsif ( ($self->syncstate == LDAP_SYNC_ADD || $self->syncstate == LDAP_SYNC_MODIFY) &&
244 ( ! defined $param->{rr} ||
245 ($param->{type} eq 'A' && defined $param->{rr} && $param->{rr} ne $self->ip) ||
246 ($param->{type} eq 'PTR' && defined $param->{rr} &&
247 $param->{rr} ne sprintf("%s.%s", $d_nam, $self->zones->[0]))
248 )
249 ) {
250  
251 $update->push( update => rr_del( $param->{type} eq 'A' ? $param->{fqdn} : $param->{reverse} ) );
252 $update->push( update => rr_add( $param->{rr_add} ) );
253  
254 } elsif ( $self->syncstate == LDAP_SYNC_DELETE ) {
255  
256 $update->push( update => rr_del( $param->{fqdn}) );
257  
258 } else {
259 $self->log->cc( pr => 'debug', fm => "%s: nothing to update", ls => [ sprintf("%s:%s",__FILE__,__LINE__) ] );
260 next;
261 }
262  
263 $update->sign_tsig($self->cf->get('service', $self->service, 'ns_keyfile'))
264 if $self->cf->is_set('service', $self->service, 'ns_keyfile');
265  
266 $self->log->cc( pr => 'warning', fm => "%s: update->string:\n%s\n",
267 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $update->string ] ) if $self->v > 2;
268  
269 $reply = $self->resolver->send($update);
270  
271 if ($reply) {
272 if ( $reply->header->rcode eq 'NOERROR' ) {
273 $self->log->cc( pr => 'debug', fm => "%s: update successful", ls => [ sprintf("%s:%s",__FILE__,__LINE__) ] );
274 } else {
275 $self->log->cc( pr => 'err', fm => "%s: update failed: %s",
276 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $reply->header->rcode ] );
277 }
278 } else {
279 $self->log->cc( pr => 'err', fm => "%s: update failed: %s",
280 ls => [ sprintf("%s:%s",__FILE__,__LINE__), $self->resolver->errorstring ] );
281 }
282  
283 undef @rr;
284 undef $update;
285 undef $param;
286 undef $reply;
287 }
288  
289 }
290  
291 =head2 ldap_sync_delete
292  
293 alias to ldap_sync_add_modify
294  
295 =cut
296  
297 sub ldap_sync_delete { goto &ldap_sync_add_modify }
298  
299 ######################################################################
300  
301 1;