source: trunk/dns-rpc.cgi@ 694

Last change on this file since 694 was 687, checked in by Kris Deugau, 9 years ago

/trunk

Roll up a couple of typofixes and missing-argument handling reviewing
dns-rpc.cgi for documentation and testing:

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