source: branches/stable/dns-rpc.cgi@ 623

Last change on this file since 623 was 548, checked in by Kris Deugau, 11 years ago

/branches/stable

Merge reverse DNS work and object conversion from /trunk, part 5

Includes changes through r543 with a few more minor conflicts.

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