source: trunk/dns-rpc.cgi@ 455

Last change on this file since 455 was 454, checked in by Kris Deugau, 12 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
Line 
1#!/usr/bin/perl -w -T
2# XMLRPC interface to manipulate most DNS DB entities
3##
4# $Id: dns-rpc.cgi 454 2013-01-16 21:43:19Z kdeugau $
5# Copyright 2012 Kris Deugau <kdeugau@deepnet.cx>
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##
20
21use strict;
22use warnings;
23
24# don't remove! required for GNU/FHS-ish install from tarball
25use lib '.'; ##uselib##
26
27use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
28use Data::Dumper;
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
41DNSDB::loadConfig(rpcflag => 1);
42
43# need to create a DNSDB object too
44my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
45 $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
46
47DNSDB::initGlobals($dbh);
48
49my $methods = {
50 'dnsdb.addDomain' => \&addDomain,
51 'dnsdb.delZone' => \&delZone,
52 'dnsdb.addRDNS' => \&addRDNS,
53 'dnsdb.addGroup' => \&addGroup,
54 'dnsdb.delGroup' => \&delGroup,
55 'dnsdb.addUser' => \&addUser,
56 'dnsdb.updateUser' => \&updateUser,
57 'dnsdb.delUser' => \&delUser,
58 'dnsdb.getLocDropdown' => \&getLocDropdown,
59 'dnsdb.getSOA' => \&getSOA,
60 'dnsdb.getRecLine' => \&getRecLine,
61 'dnsdb.getDomRecs' => \&getDomRecs,
62 'dnsdb.getRecCount' => \&getRecCount,
63 'dnsdb.addRec' => \&addRec,
64 'dnsdb.updateRec' => \&updateRec,
65 'dnsdb.addOrUpdateRevRec' => \&addOrUpdateRevRec,
66 'dnsdb.delRec' => \&delRec,
67#sub getLogCount {}
68#sub getLogEntries {}
69 'dnsdb.getRevPattern' => \&getRevPattern,
70 'dnsdb.zoneStatus' => \&zoneStatus,
71 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
72
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
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
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
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
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
147 _commoncheck(\%args, 'y');
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
154sub delZone {
155 my %args = @_;
156
157 _commoncheck(\%args, 'y');
158 die "Need forward/reverse zone flag\n" if !$args{revrec};
159
160 my ($code,$msg);
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});
164 } else {
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});
170 }
171 die $msg if $code eq 'FAIL';
172 return $msg;
173}
174
175#sub domainName {}
176#sub revName {}
177#sub domainID {}
178#sub revID {}
179
180sub addRDNS {
181 my %args = @_;
182
183 _commoncheck(\%args, 'y');
184
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';
187 return $msg; # domain ID
188}
189
190#sub getZoneCount {}
191#sub getZoneList {}
192#sub getZoneLocation {}
193
194sub addGroup {
195 my %args = @_;
196
197 _commoncheck(\%args, 'y');
198 die "Missing new group name\n" if !$args{groupname};
199 die "Missing parent group ID\n" if !$args{parent_id};
200
201# not sure how to usefully represent permissions via RPC. :/
202# not to mention, permissions are checked at the UI layer, not the DB layer.
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
215 _commoncheck(\%args, 'y');
216 die "Missing group ID or name to remove\n" if !$args{group};
217
218 my ($code,$msg);
219 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
220 if ($args{group} =~ /^\d+$/) {
221 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
222 } else {
223 my $grpid = DNSDB::groupID($dbh, $args{group});
224 die "Can't find group\n" if !$grpid;
225 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
226 }
227 die $msg if $code eq 'FAIL';
228 return $msg;
229}
230
231#sub getChildren {}
232#sub groupName {}
233#sub getGroupCount {}
234#sub getGroupList {}
235#sub groupID {}
236
237sub addUser {
238 my %args = @_;
239
240 _commoncheck(\%args, 'y');
241
242# not sure how to usefully represent permissions via RPC. :/
243# not to mention, permissions are checked at the UI layer, not the DB layer.
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
257#sub getUserCount {}
258#sub getUserList {}
259#sub getUserDropdown {}
260#sub checkUser {}
261
262sub updateUser {
263 my %args = @_;
264
265 _commoncheck(\%args, 'y');
266
267 die "Missing UID\n" if !$args{uid};
268
269 # bend and twist; get those arguments in in the right order!
270 $args{type} = 'u' if !$args{type};
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
278 my ($code,$msg) = DNSDB::updateUser($dbh, @userargs);
279 die $msg if $code eq 'FAIL';
280 return $msg;
281}
282
283sub delUser {
284 my %args = @_;
285
286 _commoncheck(\%args, 'y');
287
288 die "Missing UID\n" if !$args{uid};
289 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
290 die $msg if $code eq 'FAIL';
291 return $msg;
292}
293
294#sub userFullName {}
295#sub userStatus {}
296#sub getUserData {}
297
298#sub addLoc {}
299#sub updateLoc {}
300#sub delLoc {}
301#sub getLoc {}
302#sub getLocCount {}
303#sub getLocList {}
304
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
315sub getSOA {
316 my %args = @_;
317
318 _commoncheck(\%args);
319
320 my $ret = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{id});
321 if (!$ret) {
322 if ($args{defrec} eq 'y') {
323 die "No default SOA record in group\n";
324 } else {
325 die "No SOA record in zone\n";
326 }
327 }
328 return $ret;
329}
330
331#sub updateSOA {}
332
333sub getRecLine {
334 my %args = @_;
335
336 _commoncheck(\%args);
337
338 my $ret = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
339
340 die $DNSDB::errstr if !$ret;
341
342 return $ret;
343}
344
345sub getDomRecs {
346 my %args = @_;
347
348 _commoncheck(\%args);
349
350 # set some optional args
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
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}) );
360
361 die $DNSDB::errstr if !$ret;
362
363 return $ret;
364}
365
366sub getRecCount {
367 my %args = @_;
368
369 _commoncheck(\%args);
370
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
378 my $ret = DNSDB::getRecCount($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{filter});
379
380 die $DNSDB::errstr if !$ret;
381
382 return $ret;
383}
384
385sub addRec {
386 my %args = @_;
387
388 _commoncheck(\%args, 'y');
389
390 _loccheck(\%args);
391 _ttlcheck(\%args);
392
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
405 die $msg if $code eq 'FAIL';
406 return $msg;
407}
408
409sub updateRec {
410 my %args = @_;
411
412 _commoncheck(\%args, 'y');
413
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
421 # note dist, weight, port are not required on all types; will be ignored if not needed.
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});
426
427 die $msg if $code eq 'FAIL';
428 return $msg;
429}
430
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})) {
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);
447 my $reclist = DNSDB::getDomRecs($dbh, defrec => 'n', revrec => 'y',
448 id => $zonelist->[0]->{rdns_id}, filter => $filt);
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) {
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.
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) {
480 # only the composite and/or template types; pure PTR or nontemplate composite
481 # types are nominally impossible here.
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
492sub delRec {
493 my %args = @_;
494
495 _commoncheck(\%args, 'y');
496
497 my ($code, $msg) = DNSDB::delRec($dbh, $args{defrec}, $args{recrev}, $args{id});
498
499 die $msg if $code eq 'FAIL';
500 return $msg;
501}
502
503#sub getLogCount {}
504#sub getLogEntries {}
505
506sub getRevPattern {
507 my %args = @_;
508
509 _commoncheck(\%args, 'y');
510
511 return DNSDB::getRevPattern($dbh, $args{cidr}, $args{group});
512}
513
514#sub getTypelist {}
515#sub parentID {}
516#sub isParent {}
517
518sub zoneStatus {
519 my %args = @_;
520
521 _commoncheck(\%args, 'y');
522
523 my @arglist = ($dbh, $args{zoneid});
524 push @arglist, $args{status} if defined($args{status});
525
526 my $status = DNSDB::zoneStatus(@arglist);
527}
528
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
538#sub importAXFR {}
539#sub importBIND {}
540#sub import_tinydns {}
541#sub export {}
542#sub __export_tiny {}
543#sub _printrec_tiny {}
544#sub mailNotify {}
545
546sub get_method_list {
547 my @methods = keys %{$methods};
548 return \@methods;
549}
Note: See TracBrowser for help on using the repository browser.