source: trunk/dns-rpc.cgi@ 681

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

/trunk

Add resizeTemplate() to RPC handler. Arguably most of the code could
be moved into DNSDB.pm, but most of the core deals with records on a
record-id basis, not on a CIDR-match basis.

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