source: trunk/dns-rpc.cgi@ 675

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

/trunk

Rename internal updateRec() sub in dns-rpc.cgi to rpc_updateRec() to
make it clear which variant is getting called.

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