source: trunk/dns-rpc.cgi@ 454

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

/trunk

Tweak addOrUpdateRec for some subtle bugs. See #43.

  • handle static IP CIDRs correctly (strip the mask length)
  • make sure to check all template, composite, and bare PTR records
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 15.6 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 454 2013-01-16 21:43:19Z kdeugau $
[320]5# Copyright 2012 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##
26
[119]27use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
[121]28use Data::Dumper;
[119]29
30#use Frontier::RPC2;
31use Frontier::Responder;
32
33## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
34## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
35## that needs kicking
36#### hmm. put this in a separate file?
37#package DNSDB::RPC;
38#our @ISA = ("Frontier::RPC2", "Frontier::Responder");
39#package main;
40
[401]41DNSDB::loadConfig(rpcflag => 1);
[191]42
[119]43# need to create a DNSDB object too
[191]44my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
45 $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
46
[121]47DNSDB::initGlobals($dbh);
[119]48
49my $methods = {
50 'dnsdb.addDomain' => \&addDomain,
[401]51 'dnsdb.delZone' => \&delZone,
[405]52 'dnsdb.addRDNS' => \&addRDNS,
[121]53 'dnsdb.addGroup' => \&addGroup,
54 'dnsdb.delGroup' => \&delGroup,
55 'dnsdb.addUser' => \&addUser,
56 'dnsdb.updateUser' => \&updateUser,
57 'dnsdb.delUser' => \&delUser,
[447]58 'dnsdb.getLocDropdown' => \&getLocDropdown,
[121]59 'dnsdb.getSOA' => \&getSOA,
[123]60 'dnsdb.getRecLine' => \&getRecLine,
61 'dnsdb.getDomRecs' => \&getDomRecs,
62 'dnsdb.getRecCount' => \&getRecCount,
63 'dnsdb.addRec' => \&addRec,
[405]64 'dnsdb.updateRec' => \&updateRec,
[453]65 'dnsdb.addOrUpdateRevRec' => \&addOrUpdateRevRec,
[123]66 'dnsdb.delRec' => \&delRec,
[452]67#sub getLogCount {}
68#sub getLogEntries {}
69 'dnsdb.getRevPattern' => \&getRevPattern,
[405]70 'dnsdb.zoneStatus' => \&zoneStatus,
[452]71 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
[121]72
[119]73 'dnsdb.getMethods' => \&get_method_list
74};
75
76my $res = Frontier::Responder->new(
77 methods => $methods
78 );
79
80# "Can't do that" errors
81if (!$dbh) {
82 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
83 exit;
84}
85##fixme: fail on missing rpcuser/rpcsystem args
86
87print $res->answer;
88
89exit;
90
91##
92## Subs below here
93##
94
[401]95# Utility subs
96sub _aclcheck {
97 my $subsys = shift;
98 return 1 if grep /$ENV{REMOTE_ADDR}/, @{$DNSDB::config{rpcacl}{$subsys}};
99 return 0;
100}
101
[405]102# Let's see if we can factor these out of the RPC method subs
103sub _commoncheck {
104 my $argref = shift;
105 my $needslog = shift;
106
107 die "Missing remote system name\n" if !$argref->{rpcsystem};
108 die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
109 if ($needslog) {
110 die "Missing remote username\n" if !$argref->{rpcuser};
111 die "Couldn't set userdata for logging\n"
112 unless DNSDB::initRPC($dbh, (username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
113 fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) ) );
114 }
115}
116
[453]117# set location to the zone's default location if none is specified
118sub _loccheck {
119 my $argref = shift;
120 if (!$argref->{location} && $argref->{defrec} eq 'n') {
121 $argref->{location} = DNSDB::getZoneLocation($dbh, $argref->{revrec}, $argref->{parent_id});
122 }
123}
124
125# set ttl to zone defailt minttl if none is specified
126sub _ttlcheck {
127 my $argref = shift;
128 if (!$argref->{ttl}) {
129 my $tmp = DNSDB::getSOA($dbh, $argref->{defrec}, $argref->{revrec}, $argref->{parent_id});
130 $argref->{ttl} = $tmp->{minttl};
131 }
132}
133
[119]134#sub connectDB {
135#sub finish {
136#sub initGlobals {
137#sub initPermissions {
138#sub getPermissions {
139#sub changePermissions {
140#sub comparePermissions {
141#sub changeGroup {
142#sub _log {
143
144sub addDomain {
145 my %args = @_;
146
[405]147 _commoncheck(\%args, 'y');
[119]148
149 my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
150 die $msg if $code eq 'FAIL';
151 return $msg; # domain ID
152}
153
[401]154sub delZone {
[119]155 my %args = @_;
156
[405]157 _commoncheck(\%args, 'y');
158 die "Need forward/reverse zone flag\n" if !$args{revrec};
[119]159
[121]160 my ($code,$msg);
[405]161 # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
162 if ($args{zone} =~ /^\d+$/) {
163 ($code,$msg) = DNSDB::delZone($dbh, $args{zone}, $args{revrec});
[119]164 } else {
[405]165 my $zoneid;
166 $zoneid = DNSDB::domainID($dbh, $args{zone}) if $args{revrec} eq 'n';
167 $zoneid = DNSDB::revID($dbh, $args{zone}) if $args{revrec} eq 'y';
168 die "Can't find zone: $DNSDB::errstr\n" if !$zoneid;
169 ($code,$msg) = DNSDB::delZone($dbh, $zoneid, $args{revrec});
[119]170 }
171 die $msg if $code eq 'FAIL';
[405]172 return $msg;
[119]173}
174
[405]175#sub domainName {}
176#sub revName {}
177#sub domainID {}
178#sub revID {}
[119]179
[405]180sub addRDNS {
181 my %args = @_;
182
183 _commoncheck(\%args, 'y');
184
[447]185 my ($code, $msg) = DNSDB::addRDNS($dbh, $args{revzone}, $args{revpatt}, $args{group}, $args{state}, $args{defloc});
186 die "$msg\n" if $code eq 'FAIL';
[405]187 return $msg; # domain ID
188}
189
190#sub getZoneCount {}
191#sub getZoneList {}
192#sub getZoneLocation {}
193
[119]194sub addGroup {
195 my %args = @_;
196
[405]197 _commoncheck(\%args, 'y');
[407]198 die "Missing new group name\n" if !$args{groupname};
199 die "Missing parent group ID\n" if !$args{parent_id};
[119]200
[407]201# not sure how to usefully represent permissions via RPC. :/
[121]202# not to mention, permissions are checked at the UI layer, not the DB layer.
[119]203 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
204 record_edit => 1, record_create => 1, record_delete => 1
205 };
206## optional $inhert arg?
207 my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
208 die $msg if $code eq 'FAIL';
209 return $msg;
210}
211
212sub delGroup {
213 my %args = @_;
214
[405]215 _commoncheck(\%args, 'y');
[407]216 die "Missing group ID or name to remove\n" if !$args{group};
[119]217
[121]218 my ($code,$msg);
[119]219 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
220 if ($args{group} =~ /^\d+$/) {
[121]221 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
[119]222 } else {
223 my $grpid = DNSDB::groupID($dbh, $args{group});
[407]224 die "Can't find group\n" if !$grpid;
[121]225 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
[119]226 }
227 die $msg if $code eq 'FAIL';
[407]228 return $msg;
[119]229}
230
[405]231#sub getChildren {}
232#sub groupName {}
233#sub getGroupCount {}
234#sub getGroupList {}
235#sub groupID {}
[119]236
237sub addUser {
238 my %args = @_;
239
[405]240 _commoncheck(\%args, 'y');
[119]241
[409]242# not sure how to usefully represent permissions via RPC. :/
[121]243# not to mention, permissions are checked at the UI layer, not the DB layer.
[119]244 # bend and twist; get those arguments in in the right order!
245 $args{type} = 'u' if !$args{type};
246 $args{permstring} = 'i' if !defined($args{permstring});
247 my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
248 for my $argname ('fname','lname','phone') {
249 last if !$args{$argname};
250 push @userargs, $args{$argname};
251 }
252 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
253 die $msg if $code eq 'FAIL';
254 return $msg;
255}
256
[405]257#sub getUserCount {}
258#sub getUserList {}
259#sub getUserDropdown {}
260#sub checkUser {}
[119]261
262sub updateUser {
263 my %args = @_;
264
[405]265 _commoncheck(\%args, 'y');
[119]266
[401]267 die "Missing UID\n" if !$args{uid};
[121]268
[119]269 # bend and twist; get those arguments in in the right order!
[411]270 $args{type} = 'u' if !$args{type};
[119]271 my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
272 for my $argname ('fname','lname','phone') {
273 last if !$args{$argname};
274 push @userargs, $args{$argname};
275 }
276##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
277# have to pass them all in to be overwritten
[411]278 my ($code,$msg) = DNSDB::updateUser($dbh, @userargs);
[119]279 die $msg if $code eq 'FAIL';
[412]280 return $msg;
[119]281}
282
283sub delUser {
284 my %args = @_;
285
[405]286 _commoncheck(\%args, 'y');
[119]287
[401]288 die "Missing UID\n" if !$args{uid};
[119]289 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
290 die $msg if $code eq 'FAIL';
[412]291 return $msg;
[119]292}
293
[405]294#sub userFullName {}
295#sub userStatus {}
296#sub getUserData {}
[119]297
[405]298#sub addLoc {}
299#sub updateLoc {}
300#sub delLoc {}
301#sub getLoc {}
302#sub getLocCount {}
303#sub getLocList {}
304
[447]305sub getLocDropdown {
306 my %args = @_;
307
308 _commoncheck(\%args);
309 $args{defloc} = '' if !$args{defloc};
310
311 my $ret = DNSDB::getLocDropdown($dbh, $args{group}, $args{defloc});
312 return $ret;
313}
314
[119]315sub getSOA {
316 my %args = @_;
317
[405]318 _commoncheck(\%args);
[121]319
[413]320 my $ret = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{id});
321 if (!$ret) {
322 if ($args{defrec} eq 'y') {
[401]323 die "No default SOA record in group\n";
[121]324 } else {
[413]325 die "No SOA record in zone\n";
[121]326 }
327 }
[413]328 return $ret;
[119]329}
330
[405]331#sub updateSOA {}
332
[119]333sub getRecLine {
334 my %args = @_;
335
[405]336 _commoncheck(\%args);
[123]337
[414]338 my $ret = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
[123]339
340 die $DNSDB::errstr if !$ret;
341
342 return $ret;
[119]343}
344
345sub getDomRecs {
346 my %args = @_;
347
[405]348 _commoncheck(\%args);
[123]349
[405]350 # set some optional args
[123]351 $args{nrecs} = 'all' if !$args{nrecs};
352 $args{nstart} = 0 if !$args{nstart};
353## for order, need to map input to column names
354 $args{order} = 'host' if !$args{order};
355 $args{direction} = 'ASC' if !$args{direction};
356
[401]357 my $ret = DNSDB::getDomRecs($dbh, (defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
358 offset => $args{offset}, sortby => $args{sortby}, sortorder => $args{sortorder},
359 filter => $args{filter}) );
[123]360
361 die $DNSDB::errstr if !$ret;
362
363 return $ret;
[119]364}
365
[123]366sub getRecCount {
367 my %args = @_;
[119]368
[405]369 _commoncheck(\%args);
[123]370
[405]371 # set some optional args
372 $args{nrecs} = 'all' if !$args{nrecs};
373 $args{nstart} = 0 if !$args{nstart};
374## for order, need to map input to column names
375 $args{order} = 'host' if !$args{order};
376 $args{direction} = 'ASC' if !$args{direction};
377
[416]378 my $ret = DNSDB::getRecCount($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{filter});
[405]379
380 die $DNSDB::errstr if !$ret;
381
382 return $ret;
[123]383}
384
[119]385sub addRec {
386 my %args = @_;
387
[405]388 _commoncheck(\%args, 'y');
[123]389
[453]390 _loccheck(\%args);
391 _ttlcheck(\%args);
[123]392
[426]393 my @recargs = ($dbh, $args{defrec}, $args{revrec}, $args{parent_id},
394 \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location});
395 if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
396 push @recargs, $args{distance};
397 if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
398 push @recargs, $args{weight};
399 push @recargs, $args{port};
400 }
401 }
402
403 my ($code, $msg) = DNSDB::addRec(@recargs);
404
[123]405 die $msg if $code eq 'FAIL';
[426]406 return $msg;
[119]407}
408
409sub updateRec {
410 my %args = @_;
411
[405]412 _commoncheck(\%args, 'y');
[123]413
[452]414 # get old line, so we can update only the bits that the caller passed to change
415 # note we subbed address for val since it's a little more caller-friendly
416 my $oldrec = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
417 foreach my $field (qw(name type address ttl location distance weight port)) {
418 $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
419 }
420
[405]421 # note dist, weight, port are not required on all types; will be ignored if not needed.
[426]422 # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
423 my ($code, $msg) = DNSDB::updateRec($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
424 \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
425 $args{distance}, $args{weight}, $args{port});
[123]426
427 die $msg if $code eq 'FAIL';
[426]428 return $msg;
[119]429}
430
[453]431# Takes a passed CIDR block and DNS pattern; adds a new record or updates the record(s) affected
432sub addOrUpdateRevRec {
433 my %args = @_;
434
435 _commoncheck(\%args, 'y');
436 $args{cidr} = new NetAddr::IP $args{cidr};
437
438 my $zonelist = DNSDB::getZonesByCIDR($dbh, %args);
439 if (scalar(@$zonelist) == 0) {
440 # enhh.... WTF?
441 } elsif (scalar(@$zonelist) == 1) {
442 # check if the single zone returned is bigger than the CIDR. if so, we can just add a record
443 my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
444 if ($zone->contains($args{cidr})) {
[454]445 # We need to strip the CIDR mask on IPv4 /32 assignments, or we just add a new record all the time.
446 my $filt = ($args{cidr}->{isv6} || $args{cidr}->masklen != 32 ? "$args{cidr}" : $args{cidr}->addr);
[453]447 my $reclist = DNSDB::getDomRecs($dbh, defrec => 'n', revrec => 'y',
[454]448 id => $zonelist->[0]->{rdns_id}, filter => $filt);
[453]449 if (scalar(@$reclist) == 0) {
450 # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
451 my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
452 addRec(defrec =>'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
453 address => "$args{cidr}", %args);
454 } else {
455 foreach my $rec (@$reclist) {
[454]456 # pure PTR plus composite types
457 next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281
458 || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
459 next unless $rec->{val} eq $filt; # make sure we really update the record we want to update.
[453]460 updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
461 parent_id => $zonelist->[0]->{rdns_id}, %args);
462 last; # only do one record.
463 }
464 }
465 } else {
466 # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
467 } # done single-zone-contains-$cidr
468 } else {
469 # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
470 # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
471 foreach my $zdata (@$zonelist) {
472 my $reclist = DNSDB::getDomRecs($dbh, defrec => 'n', revrec => 'y',
473 id => $zdata->{rdns_id}, filter => $zdata->{revnet});
474 if (scalar(@$reclist) == 0) {
475 my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
476 addRec(defrec =>'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
477 address => "$args{cidr}", %args);
478 } else {
479 foreach my $rec (@$reclist) {
[454]480 # only the composite and/or template types; pure PTR or nontemplate composite
481 # types are nominally impossible here.
[453]482 next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
483 updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
484 parent_id => $zdata->{rdns_id}, %args);
485 last; # only do one record.
486 }
487 }
488 } # iterate zones within $cidr
489 } # done $cidr-contains-zones
490}
491
[119]492sub delRec {
493 my %args = @_;
494
[405]495 _commoncheck(\%args, 'y');
[123]496
[426]497 my ($code, $msg) = DNSDB::delRec($dbh, $args{defrec}, $args{recrev}, $args{id});
[123]498
499 die $msg if $code eq 'FAIL';
[426]500 return $msg;
[119]501}
502
[405]503#sub getLogCount {}
504#sub getLogEntries {}
[452]505
506sub getRevPattern {
507 my %args = @_;
508
509 _commoncheck(\%args, 'y');
510
511 return DNSDB::getRevPattern($dbh, $args{cidr}, $args{group});
512}
513
[405]514#sub getTypelist {}
515#sub parentID {}
516#sub isParent {}
[123]517
[405]518sub zoneStatus {
[123]519 my %args = @_;
520
[405]521 _commoncheck(\%args, 'y');
[123]522
[405]523 my @arglist = ($dbh, $args{zoneid});
[123]524 push @arglist, $args{status} if defined($args{status});
525
[405]526 my $status = DNSDB::zoneStatus(@arglist);
[123]527}
528
[452]529# Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
530sub getZonesByCIDR {
531 my %args = @_;
532
533 _commoncheck(\%args, 'y');
534
535 return DNSDB::getZonesByCIDR($dbh, %args);
536}
537
[405]538#sub importAXFR {}
539#sub importBIND {}
540#sub import_tinydns {}
541#sub export {}
542#sub __export_tiny {}
543#sub _printrec_tiny {}
544#sub mailNotify {}
[119]545
546sub get_method_list {
547 my @methods = keys %{$methods};
548 return \@methods;
549}
Note: See TracBrowser for help on using the repository browser.