source: trunk/dns-rpc.cgi@ 514

Last change on this file since 514 was 511, checked in by Kris Deugau, 12 years ago

/trunk

Clean up some error-log garbage in dns-rpc.cgi. See #25.

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