source: trunk/dns-rpc.cgi@ 678

Last change on this file since 678 was 678, checked in by Kris Deugau, 10 years ago

/trunk

Fixes for edge-case updates via RPC:

  • Replace call to $dnsdb->updateRec with rpc_UpdateRec in addOrUpdateRevRec to match the way arguments were being passed, and for consistency with the rest of the internal subcalls.
  • Accept CIDR addresses, not just bare IPs, in updateRevSet; RPC is inexplicably slow so allowing batching of similar or mostly similar operations could speed things up a lot
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 25.0 KB
Line 
1#!/usr/bin/perl -w -T
2# XMLRPC interface to manipulate most DNS DB entities
3##
4# $Id: dns-rpc.cgi 678 2015-01-22 23:27:21Z kdeugau $
5# Copyright 2012,2013 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21use strict;
22use warnings;
23
24# don't remove! required for GNU/FHS-ish install from tarball
25use lib '.'; ##uselib##
26use DNSDB;
27
28use FCGI;
29#use Frontier::RPC2;
30use Frontier::Responder;
31
32## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
33## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
34## that needs kicking
35#### hmm. put this in a separate file?
36#package DNSDB::RPC;
37#our @ISA = ("Frontier::RPC2", "Frontier::Responder");
38#package main;
39
40my $dnsdb = DNSDB->new();
41
42my $methods = {
43#sub getPermissions {
44#sub changePermissions {
45#sub comparePermissions {
46#sub changeGroup {
47 'dnsdb.addDomain' => \&addDomain,
48 'dnsdb.delZone' => \&delZone,
49#sub domainName {
50#sub revName {
51 'dnsdb.domainID' => \&domainID,
52#sub revID {
53 'dnsdb.addRDNS' => \&addRDNS,
54#sub getZoneCount {
55#sub getZoneList {
56#sub getZoneLocation {
57 'dnsdb.addGroup' => \&addGroup,
58 'dnsdb.delGroup' => \&delGroup,
59#sub getChildren {
60#sub groupName {
61#sub getGroupCount {
62#sub getGroupList {
63#sub groupID {
64 'dnsdb.addUser' => \&addUser,
65#sub getUserCount {
66#sub getUserList {
67#sub getUserDropdown {
68 'dnsdb.updateUser' => \&updateUser,
69 'dnsdb.delUser' => \&delUser,
70#sub userFullName {
71#sub userStatus {
72#sub getUserData {
73#sub addLoc {
74#sub updateLoc {
75#sub delLoc {
76#sub getLoc {
77#sub getLocCount {
78#sub getLocList {
79 'dnsdb.getLocDropdown' => \&getLocDropdown,
80 'dnsdb.getSOA' => \&getSOA,
81#sub updateSOA {
82 'dnsdb.getRecLine' => \&getRecLine,
83 'dnsdb.getRecList' => \&getRecList,
84 'dnsdb.getRecCount' => \&getRecCount,
85 'dnsdb.addRec' => \&rpc_addRec,
86 'dnsdb.updateRec' => \&rpc_updateRec,
87#sub downconvert {
88 'dnsdb.addOrUpdateRevRec' => \&addOrUpdateRevRec,
89 'dnsdb.updateRevSet' => \&updateRevSet,
90 'dnsdb.delRec' => \&delRec,
91 'dnsdb.delByCIDR' => \&delByCIDR,
92#sub getLogCount {}
93#sub getLogEntries {}
94 'dnsdb.getRevPattern' => \&getRevPattern,
95 'dnsdb.getRevSet' => \&getRevSet,
96 'dnsdb.getTypelist' => \&getTypelist,
97 'dnsdb.getTypemap' => \&getTypemap,
98 'dnsdb.getReverse_typemap' => \&getReverse_typemap,
99#sub parentID {
100#sub isParent {
101 'dnsdb.zoneStatus' => \&zoneStatus,
102 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
103#sub importAXFR {
104#sub importBIND {
105#sub import_tinydns {
106#sub export {
107#sub mailNotify {
108
109 'dnsdb.getMethods' => \&get_method_list
110};
111
112my $reqcnt = 0;
113
114while (FCGI::accept >= 0) {
115 my $res = Frontier::Responder->new(
116 methods => $methods
117 );
118
119 # "Can't do that" errors
120 if (!$dnsdb) {
121 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $dnsdb->err);
122 } else {
123 print $res->answer;
124 }
125 last if $reqcnt++ > $dnsdb->{maxfcgi};
126} # while FCGI::accept
127
128
129exit;
130
131##
132## Subs below here
133##
134
135# Check RPC ACL
136sub _aclcheck {
137 my $subsys = shift;
138 return 1 if grep /$ENV{REMOTE_ADDR}/, @{$dnsdb->{rpcacl}{$subsys}};
139 warn "$subsys/$ENV{REMOTE_ADDR} not in ACL\n"; # a bit of logging
140 return 0;
141}
142
143# Let's see if we can factor these out of the RPC method subs
144sub _commoncheck {
145 my $argref = shift;
146 my $needslog = shift;
147
148 die "Missing remote system name\n" if !$argref->{rpcsystem};
149 die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
150 if ($needslog) {
151 die "Missing remote username\n" if !$argref->{rpcuser};
152 die "Couldn't set userdata for logging\n"
153 unless $dnsdb->initRPC(username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
154 fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) );
155 }
156}
157
158# check for defrec and revrec; only call on subs that deal with records
159sub _reccheck {
160 my $argref = shift;
161 die "Missing defrec and/or revrec flags\n" if !($argref->{defrec} || $argref->{revrec});
162}
163
164# set location to the zone's default location if none is specified
165sub _loccheck {
166 my $argref = shift;
167 if (!$argref->{location} && $argref->{defrec} eq 'n') {
168 $argref->{location} = $dnsdb->getZoneLocation($argref->{revrec}, $argref->{parent_id});
169 }
170}
171
172# set ttl to zone defailt minttl if none is specified
173sub _ttlcheck {
174 my $argref = shift;
175 if (!$argref->{ttl}) {
176 my $tmp = $dnsdb->getSOA($argref->{defrec}, $argref->{revrec}, $argref->{parent_id});
177 $argref->{ttl} = $tmp->{minttl};
178 }
179}
180
181#sub connectDB {
182#sub finish {
183#sub initGlobals {
184#sub initPermissions {
185#sub getPermissions {
186#sub changePermissions {
187#sub comparePermissions {
188#sub changeGroup {
189#sub _log {
190
191sub addDomain {
192 my %args = @_;
193
194 _commoncheck(\%args, 'y');
195
196 my ($code, $msg) = $dnsdb->addDomain($args{domain}, $args{group}, $args{state}, $args{location});
197 die "$msg\n" if $code eq 'FAIL';
198 return $msg; # domain ID
199}
200
201sub delZone {
202 my %args = @_;
203
204 _commoncheck(\%args, 'y');
205 die "Need forward/reverse zone flag\n" if !$args{revrec};
206
207 my ($code,$msg);
208 # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
209 if ($args{zone} =~ /^\d+$/) {
210 ($code,$msg) = $dnsdb->delZone($args{zone}, $args{revrec});
211 } else {
212 my $zoneid;
213 $zoneid = $dnsdb->domainID($args{zone}) if $args{revrec} eq 'n';
214 $zoneid = $dnsdb->revID($args{zone}) if $args{revrec} eq 'y';
215 die "Can't find zone: ".$dnsdb->errstr."\n" if !$zoneid;
216 ($code,$msg) = $dnsdb->delZone($zoneid, $args{revrec});
217 }
218 die "$msg\n" if $code eq 'FAIL';
219 return $msg;
220}
221
222#sub domainName {}
223#sub revName {}
224
225sub domainID {
226 my %args = @_;
227
228 _commoncheck(\%args, 'y');
229
230 my $domid = $dnsdb->domainID($args{domain});
231 die $dnsdb->errstr."\n" if !$domid;
232 return $domid;
233}
234
235#sub revID {}
236
237sub addRDNS {
238 my %args = @_;
239
240 _commoncheck(\%args, 'y');
241
242 my ($code, $msg) = $dnsdb->addRDNS($args{revzone}, $args{revpatt}, $args{group}, $args{state}, $args{defloc});
243 die "$msg\n" if $code eq 'FAIL';
244 return $msg; # domain ID
245}
246
247#sub getZoneCount {}
248#sub getZoneList {}
249#sub getZoneLocation {}
250
251sub addGroup {
252 my %args = @_;
253
254 _commoncheck(\%args, 'y');
255 die "Missing new group name\n" if !$args{groupname};
256 die "Missing parent group ID\n" if !$args{parent_id};
257
258# not sure how to usefully represent permissions via RPC. :/
259# not to mention, permissions are checked at the UI layer, not the DB layer.
260 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
261 record_edit => 1, record_create => 1, record_delete => 1
262 };
263## optional $inhert arg?
264 my ($code,$msg) = $dnsdb->addGroup($args{groupname}, $args{parent_id}, $perms);
265 die "$msg\n" if $code eq 'FAIL';
266 return $msg;
267}
268
269sub delGroup {
270 my %args = @_;
271
272 _commoncheck(\%args, 'y');
273 die "Missing group ID or name to remove\n" if !$args{group};
274
275 my ($code,$msg);
276 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
277 if ($args{group} =~ /^\d+$/) {
278 ($code,$msg) = $dnsdb->delGroup($args{group});
279 } else {
280 my $grpid = $dnsdb->groupID($args{group});
281 die "Can't find group\n" if !$grpid;
282 ($code,$msg) = $dnsdb->delGroup($grpid);
283 }
284 die "$msg\n" if $code eq 'FAIL';
285 return $msg;
286}
287
288#sub getChildren {}
289#sub groupName {}
290#sub getGroupCount {}
291#sub getGroupList {}
292#sub groupID {}
293
294sub addUser {
295 my %args = @_;
296
297 _commoncheck(\%args, 'y');
298
299# not sure how to usefully represent permissions via RPC. :/
300# not to mention, permissions are checked at the UI layer, not the DB layer.
301 # bend and twist; get those arguments in in the right order!
302 $args{type} = 'u' if !$args{type};
303 $args{permstring} = 'i' if !defined($args{permstring});
304 my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
305 for my $argname ('fname','lname','phone') {
306 last if !$args{$argname};
307 push @userargs, $args{$argname};
308 }
309 my ($code,$msg) = $dnsdb->addUser(@userargs);
310 die "$msg\n" if $code eq 'FAIL';
311 return $msg;
312}
313
314#sub getUserCount {}
315#sub getUserList {}
316#sub getUserDropdown {}
317#sub checkUser {}
318
319sub updateUser {
320 my %args = @_;
321
322 _commoncheck(\%args, 'y');
323
324 die "Missing UID\n" if !$args{uid};
325
326 # bend and twist; get those arguments in in the right order!
327 $args{type} = 'u' if !$args{type};
328 my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
329 for my $argname ('fname','lname','phone') {
330 last if !$args{$argname};
331 push @userargs, $args{$argname};
332 }
333##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
334# have to pass them all in to be overwritten
335 my ($code,$msg) = $dnsdb->updateUser(@userargs);
336 die "$msg\n" if $code eq 'FAIL';
337 return $msg;
338}
339
340sub delUser {
341 my %args = @_;
342
343 _commoncheck(\%args, 'y');
344
345 die "Missing UID\n" if !$args{uid};
346 my ($code,$msg) = $dnsdb->delUser($args{uid});
347 die "$msg\n" if $code eq 'FAIL';
348 return $msg;
349}
350
351#sub userFullName {}
352#sub userStatus {}
353#sub getUserData {}
354
355#sub addLoc {}
356#sub updateLoc {}
357#sub delLoc {}
358#sub getLoc {}
359#sub getLocCount {}
360#sub getLocList {}
361
362sub getLocDropdown {
363 my %args = @_;
364
365 _commoncheck(\%args);
366 $args{defloc} = '' if !$args{defloc};
367
368 my $ret = $dnsdb->getLocDropdown($args{group}, $args{defloc});
369 return $ret;
370}
371
372sub getSOA {
373 my %args = @_;
374
375 _commoncheck(\%args);
376
377 _reccheck(\%args);
378
379 my $ret = $dnsdb->getSOA($args{defrec}, $args{revrec}, $args{id});
380 if (!$ret) {
381 if ($args{defrec} eq 'y') {
382 die "No default SOA record in group\n";
383 } else {
384 die "No SOA record in zone\n";
385 }
386 }
387 return $ret;
388}
389
390#sub updateSOA {}
391
392sub getRecLine {
393 my %args = @_;
394
395 _commoncheck(\%args);
396
397 _reccheck(\%args);
398
399 my $ret = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
400
401 die $dnsdb->errstr."\n" if !$ret;
402
403 return $ret;
404}
405
406sub getRecList {
407 my %args = @_;
408
409 _commoncheck(\%args);
410
411 # deal gracefully with alternate calling convention for args{id}
412 $args{id} = $args{ID} if !$args{id} && $args{ID};
413 # ... and fail if we don't have one
414 die "Missing zone ID\n" if !$args{id};
415
416 # set some optional args
417 $args{offset} = 0 if !$args{offset};
418## for order, need to map input to column names
419 $args{order} = 'host' if !$args{order};
420 $args{direction} = 'ASC' if !$args{direction};
421 $args{defrec} = 'n' if !$args{defrec};
422 $args{revrec} = 'n' if !$args{revrec};
423
424 # convert zone name to zone ID, if needed
425 if ($args{defrec} eq 'n') {
426 if ($args{revrec} eq 'n') {
427 $args{id} = $dnsdb->domainID($args{id}) if $args{id} !~ /^\d+$/;
428 } else {
429 $args{id} = $dnsdb->revID($args{id}) if $args{id} !~ /^\d+$/
430 }
431 }
432
433 # fail if we *still* don't have a valid zone ID
434 die $dnsdb->errstr."\n" if !$args{id};
435
436 # and finally retrieve the records.
437 my $ret = $dnsdb->getRecList(defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
438 offset => $args{offset}, nrecs => $args{nrecs}, sortby => $args{sortby},
439 sortorder => $args{sortorder}, filter => $args{filter});
440 die $dnsdb->errstr."\n" if !$ret;
441
442 return $ret;
443}
444
445sub getRecCount {
446 my %args = @_;
447
448 _commoncheck(\%args);
449
450 _reccheck(\%args);
451
452 # set some optional args
453 $args{nrecs} = 'all' if !$args{nrecs};
454 $args{nstart} = 0 if !$args{nstart};
455## for order, need to map input to column names
456 $args{order} = 'host' if !$args{order};
457 $args{direction} = 'ASC' if !$args{direction};
458
459 my $ret = $dnsdb->getRecCount(defrec => $args{defrec}, revrec => $args{revrec},
460 id => $args{id}, filter => $args{filter});
461
462 die $dnsdb->errstr."\n" if !$ret;
463
464 return $ret;
465}
466
467sub rpc_addRec {
468 my %args = @_;
469
470 _commoncheck(\%args, 'y');
471
472 _reccheck(\%args);
473 _loccheck(\%args);
474 _ttlcheck(\%args);
475
476 # allow passing text types rather than DNS integer IDs
477 $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
478
479 my @recargs = ($args{defrec}, $args{revrec}, $args{parent_id},
480 \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
481 $args{expires}, $args{stamp});
482 if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
483 push @recargs, $args{distance};
484 if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
485 push @recargs, $args{weight};
486 push @recargs, $args{port};
487 }
488 }
489
490 my ($code, $msg) = $dnsdb->addRec(@recargs);
491
492 die "$msg\n" if $code eq 'FAIL';
493 return $msg;
494} # rpc_addRec
495
496sub rpc_updateRec {
497 my %args = @_;
498
499 _commoncheck(\%args, 'y');
500
501 _reccheck(\%args);
502
503 # put some caller-friendly names in their rightful DB column places
504 $args{val} = $args{address};
505 $args{host} = $args{name};
506
507 # get old line, so we can update only the bits that the caller passed to change
508 my $oldrec = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
509 foreach my $field (qw(host type val ttl location expires distance weight port)) {
510 $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
511 }
512 # stamp has special handling when blank or 0. "undefined" from the caller should mean "don't change"
513 $args{stamp} = $oldrec->{stamp} if !defined($args{stamp}) && $oldrec->{stampactive};
514
515 # allow passing text types rather than DNS integer IDs
516 $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
517
518 # note dist, weight, port are not required on all types; will be ignored if not needed.
519 # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
520 my ($code, $msg) = $dnsdb->updateRec($args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
521 \$args{host}, \$args{type}, \$args{val}, $args{ttl}, $args{location},
522 $args{expires}, $args{stamp},
523 $args{distance}, $args{weight}, $args{port});
524
525 die "$msg\n" if $code eq 'FAIL';
526 return $msg;
527}
528
529# Takes a passed CIDR block and DNS pattern; adds a new record or updates the record(s) affected
530sub addOrUpdateRevRec {
531 my %args = @_;
532
533 _commoncheck(\%args, 'y');
534 my $cidr = new NetAddr::IP $args{cidr};
535
536 my $zonelist = $dnsdb->getZonesByCIDR(%args);
537 if (scalar(@$zonelist) == 0) {
538 # enhh.... WTF?
539 } elsif (scalar(@$zonelist) == 1) {
540 # check if the single zone returned is bigger than the CIDR. if so, we can just add a record
541 my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
542 if ($zone->contains($cidr)) {
543 # We need to strip the CIDR mask on IPv4 /32 assignments, or we just add a new record all the time.
544 my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
545 my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
546 id => $zonelist->[0]->{rdns_id}, filter => $filt);
547##fixme: Figure some new magic to automerge new incoming A(AAA)+PTR requests
548# with existing A records to prevent duplication of A(AAA) records
549 if (scalar(@$reclist) == 0) {
550 # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
551 my $type = ($cidr->{isv6} ? ($cidr->masklen == 128 ? 65281 : 65284) : ($cidr->masklen == 32 ? 65280 : 65283) );
552 rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
553 address => "$cidr", %args);
554 } else {
555 my $flag = 0;
556 foreach my $rec (@$reclist) {
557 # pure PTR plus composite types
558 next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281
559 || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
560 next unless $rec->{val} eq $filt; # make sure we really update the record we want to update.
561 rpc_updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
562 parent_id => $zonelist->[0]->{rdns_id}, address => "$cidr", %args);
563 $flag = 1;
564 last; # only do one record.
565 }
566 unless ($flag) {
567 # Nothing was updated, so we didn't really have a match. Add as per @$reclist==0
568 # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
569 my $type = ($cidr->{isv6} ? 65282 : ($cidr->masklen == 32 ? 65280 : 65283) );
570 rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
571 address => "$cidr", %args);
572 }
573 }
574 } else {
575 # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
576 } # done single-zone-contains-$cidr
577 } else {
578 # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
579 # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
580 foreach my $zdata (@$zonelist) {
581 my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
582 id => $zdata->{rdns_id}, filter => $zdata->{revnet});
583 if (scalar(@$reclist) == 0) {
584 my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
585 rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
586 address => "$args{cidr}", %args);
587 } else {
588 foreach my $rec (@$reclist) {
589 # only the composite and/or template types; pure PTR or nontemplate composite
590 # types are nominally impossible here.
591 next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
592 rpc_updateRec(defrec => 'n', revrec => 'y', id => $rec->{record_id},
593 parent_id => $zdata->{rdns_id}, %args);
594 last; # only do one record.
595 }
596 }
597 } # iterate zones within $cidr
598 } # done $cidr-contains-zones
599##fixme: what about errors? what about warnings?
600} # done addOrUpdateRevRec()
601
602# Update rDNS on a whole batch of IP addresses. Presented as a separate sub via RPC
603# since RPC calls can be s...l...o...w....
604sub updateRevSet {
605 my %args = @_;
606
607 _commoncheck(\%args, 'y');
608
609 my @ret;
610 # loop over passed IP/hostname pairs
611 foreach my $key (keys %args) {
612 next unless $key =~ m{^host_((?:[\d.]+|[\da-f:]+)(?:/\d+)?)$};
613 my $ip = $1;
614 push @ret, addOrUpdateRevRec(cidr => $ip, name => $args{$key}, %args);
615 }
616##fixme: what about errors? what about warnings?
617 return \@ret;
618} # done updateRevSet()
619
620sub delRec {
621 my %args = @_;
622
623 _commoncheck(\%args, 'y');
624
625 _reccheck(\%args);
626
627 my ($code, $msg) = $dnsdb->delRec($args{defrec}, $args{recrev}, $args{id});
628
629 die "$msg\n" if $code eq 'FAIL';
630 return $msg;
631}
632
633sub delByCIDR {
634 my %args = @_;
635
636 _commoncheck(\%args, 'y');
637
638 # much like addOrUpdateRevRec()
639 my $zonelist = $dnsdb->getZonesByCIDR(%args);
640 my $cidr = new NetAddr::IP $args{cidr};
641
642 if (scalar(@$zonelist) == 0) {
643 # enhh.... WTF?
644 } elsif (scalar(@$zonelist) == 1) {
645
646 # check if the single zone returned is bigger than the CIDR
647 my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
648 if ($zone->contains($cidr)) {
649
650 if ($args{delsubs}) {
651 # Delete ALL EVARYTHING!!one11!! in $args{cidr}
652 my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zonelist->[0]->{rdns_id});
653 foreach my $rec (@$reclist) {
654 my $reccidr = new NetAddr::IP $rec->{val};
655 next unless $cidr->contains($reccidr);
656 next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
657 $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
658 ##fixme: multiple records, wanna wax'em all, how to report errors?
659 if ($args{delforward} ||
660 $rec->{type} == 12 || $rec->{type} == 65282 ||
661 $rec->{type} == 65283 || $rec->{type} == 65284) {
662 my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
663 } else {
664 my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
665 }
666 }
667 if ($args{parpatt} && $zone == $cidr) {
668 # Edge case; we've just gone and axed all the records in the reverse zone.
669 # Re-add one to match the parent if we've been given a pattern to use.
670 rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id},
671 type => ($zone->{isv6} ? 65284 : 65283), address => "$cidr", name => $args{parpatt}, %args);
672 }
673
674 } else {
675 # Selectively delete only exact matches on $args{cidr}
676
677 # We need to strip the CIDR mask on IPv4 /32 assignments, or we can't find single-IP records
678 my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
679 my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
680 id => $zonelist->[0]->{rdns_id}, filter => $filt, sortby => 'val', sortorder => 'DESC');
681 foreach my $rec (@$reclist) {
682 my $reccidr = new NetAddr::IP $rec->{val};
683 next unless $cidr == $reccidr;
684 next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
685 $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
686 if ($args{delforward} || $rec->{type} == 12) {
687 my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
688 die "$msg\n" if $code eq 'FAIL';
689 return $msg;
690 } else {
691 my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
692 die $dnsdb->errstr."\n" if !$ret;
693 return "A+PTR for $args{cidr} split and PTR removed";
694 }
695 } # foreach @$reclist
696 }
697
698 } else { # $cidr > $zone but we only have one zone
699 # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
700 return "Warning: $args{cidr} is only partly represented in DNS. Check and remove DNS records manually.";
701 } # done single-zone-contains-$cidr
702
703 } else { # multiple zones nominally "contain" $cidr
704 # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
705 # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
706 foreach my $zdata (@$zonelist) {
707 my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zdata->{rdns_id});
708 if (scalar(@$reclist) == 0) {
709# nothing to do? or do we (re)add a record based on the parent?
710# yes, yes we do, past the close of the else
711# my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
712# rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
713# address => "$args{cidr}", %args);
714 } else {
715 foreach my $rec (@$reclist) {
716 next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
717 $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
718 # Template types are only useful when attached to a reverse zone.
719##fixme ..... or ARE THEY?
720 if ($args{delforward} ||
721 $rec->{type} == 12 || $rec->{type} == 65282 ||
722 $rec->{type} == 65283 || $rec->{type} == 65284) {
723 my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
724 } else {
725 my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
726 }
727 } # foreach @$reclist
728 } # nrecs != 0
729 if ($args{parpatt}) {
730 # We've just gone and axed all the records in the reverse zone.
731 # Re-add one to match the parent if we've been given a pattern to use.
732 rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id},
733 type => ($cidr->{isv6} ? 65284 : 65283),
734 address => $zdata->{revnet}, name => $args{parpatt}, %args);
735 }
736 } # iterate zones within $cidr
737 } # done $cidr-contains-zones
738
739} # end delByCIDR()
740
741#sub getLogCount {}
742#sub getLogEntries {}
743
744sub getRevPattern {
745 my %args = @_;
746
747 _commoncheck(\%args, 'y');
748
749 return $dnsdb->getRevPattern($args{cidr}, $args{group});
750}
751
752sub getRevSet {
753 my %args = @_;
754
755 _commoncheck(\%args, 'y');
756
757 return $dnsdb->getRevSet($args{cidr}, $args{group});
758}
759
760sub getTypelist {
761 my %args = @_;
762 _commoncheck(\%args, 'y');
763
764 $args{selected} = $reverse_typemap{A} if !$args{selected};
765
766 return $dnsdb->getTypelist($args{recgroup}, $args{selected});
767}
768
769sub getTypemap {
770 my %args = @_;
771 _commoncheck(\%args, 'y');
772 return \%typemap;
773}
774
775sub getReverse_typemap {
776 my %args = @_;
777 _commoncheck(\%args, 'y');
778 return \%reverse_typemap;
779}
780
781#sub parentID {}
782#sub isParent {}
783
784sub zoneStatus {
785 my %args = @_;
786
787 _commoncheck(\%args, 'y');
788
789 my @arglist = ($args{zoneid});
790 push @arglist, $args{status} if defined($args{status});
791
792 my $status = $dnsdb->zoneStatus(@arglist);
793}
794
795# Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
796sub getZonesByCIDR {
797 my %args = @_;
798
799 _commoncheck(\%args, 'y');
800
801 return $dnsdb->getZonesByCIDR(%args);
802}
803
804#sub importAXFR {}
805#sub importBIND {}
806#sub import_tinydns {}
807#sub export {}
808#sub __export_tiny {}
809#sub _printrec_tiny {}
810#sub mailNotify {}
811
812sub get_method_list {
813 my @methods = keys %{$methods};
814 return \@methods;
815}
Note: See TracBrowser for help on using the repository browser.