source: trunk/dns-rpc.cgi@ 452

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

/trunk

Continue tweaking RPC for actual usage. See #43.

  • Add and expose getRevPattern() to retrieve the narrowest template pattern applicable to a passed CIDR (netblock or IP)
  • Add and expose getZonesByCIDR() to retrieve a list of revzones and zone IDs for records within a passed CIDR block. Note the block may be larger than the zones, so we may return more than one revzone.
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 12.4 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 452 2013-01-11 17:21:13Z 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,
[123]65 'dnsdb.delRec' => \&delRec,
[452]66#sub getLogCount {}
67#sub getLogEntries {}
68 'dnsdb.getRevPattern' => \&getRevPattern,
[405]69 'dnsdb.zoneStatus' => \&zoneStatus,
[452]70 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
[121]71
[119]72 'dnsdb.getMethods' => \&get_method_list
73};
74
75my $res = Frontier::Responder->new(
76 methods => $methods
77 );
78
79# "Can't do that" errors
80if (!$dbh) {
81 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
82 exit;
83}
84##fixme: fail on missing rpcuser/rpcsystem args
85
86print $res->answer;
87
88exit;
89
90##
91## Subs below here
92##
93
[401]94# Utility subs
95sub _aclcheck {
96 my $subsys = shift;
97 return 1 if grep /$ENV{REMOTE_ADDR}/, @{$DNSDB::config{rpcacl}{$subsys}};
98 return 0;
99}
100
[405]101# Let's see if we can factor these out of the RPC method subs
102sub _commoncheck {
103 my $argref = shift;
104 my $needslog = shift;
105
106 die "Missing remote system name\n" if !$argref->{rpcsystem};
107 die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
108 if ($needslog) {
109 die "Missing remote username\n" if !$argref->{rpcuser};
110 die "Couldn't set userdata for logging\n"
111 unless DNSDB::initRPC($dbh, (username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
112 fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) ) );
113 }
114}
115
[119]116#sub connectDB {
117#sub finish {
118#sub initGlobals {
119#sub initPermissions {
120#sub getPermissions {
121#sub changePermissions {
122#sub comparePermissions {
123#sub changeGroup {
124#sub _log {
125
126sub addDomain {
127 my %args = @_;
128
[405]129 _commoncheck(\%args, 'y');
[119]130
131 my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
132 die $msg if $code eq 'FAIL';
133 return $msg; # domain ID
134}
135
[401]136sub delZone {
[119]137 my %args = @_;
138
[405]139 _commoncheck(\%args, 'y');
140 die "Need forward/reverse zone flag\n" if !$args{revrec};
[119]141
[121]142 my ($code,$msg);
[405]143 # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
144 if ($args{zone} =~ /^\d+$/) {
145 ($code,$msg) = DNSDB::delZone($dbh, $args{zone}, $args{revrec});
[119]146 } else {
[405]147 my $zoneid;
148 $zoneid = DNSDB::domainID($dbh, $args{zone}) if $args{revrec} eq 'n';
149 $zoneid = DNSDB::revID($dbh, $args{zone}) if $args{revrec} eq 'y';
150 die "Can't find zone: $DNSDB::errstr\n" if !$zoneid;
151 ($code,$msg) = DNSDB::delZone($dbh, $zoneid, $args{revrec});
[119]152 }
153 die $msg if $code eq 'FAIL';
[405]154 return $msg;
[119]155}
156
[405]157#sub domainName {}
158#sub revName {}
159#sub domainID {}
160#sub revID {}
[119]161
[405]162sub addRDNS {
163 my %args = @_;
164
165 _commoncheck(\%args, 'y');
166
[447]167 my ($code, $msg) = DNSDB::addRDNS($dbh, $args{revzone}, $args{revpatt}, $args{group}, $args{state}, $args{defloc});
168 die "$msg\n" if $code eq 'FAIL';
[405]169 return $msg; # domain ID
170}
171
172#sub getZoneCount {}
173#sub getZoneList {}
174#sub getZoneLocation {}
175
[119]176sub addGroup {
177 my %args = @_;
178
[405]179 _commoncheck(\%args, 'y');
[407]180 die "Missing new group name\n" if !$args{groupname};
181 die "Missing parent group ID\n" if !$args{parent_id};
[119]182
[407]183# not sure how to usefully represent permissions via RPC. :/
[121]184# not to mention, permissions are checked at the UI layer, not the DB layer.
[119]185 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
186 record_edit => 1, record_create => 1, record_delete => 1
187 };
188## optional $inhert arg?
189 my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
190 die $msg if $code eq 'FAIL';
191 return $msg;
192}
193
194sub delGroup {
195 my %args = @_;
196
[405]197 _commoncheck(\%args, 'y');
[407]198 die "Missing group ID or name to remove\n" if !$args{group};
[119]199
[121]200 my ($code,$msg);
[119]201 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
202 if ($args{group} =~ /^\d+$/) {
[121]203 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
[119]204 } else {
205 my $grpid = DNSDB::groupID($dbh, $args{group});
[407]206 die "Can't find group\n" if !$grpid;
[121]207 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
[119]208 }
209 die $msg if $code eq 'FAIL';
[407]210 return $msg;
[119]211}
212
[405]213#sub getChildren {}
214#sub groupName {}
215#sub getGroupCount {}
216#sub getGroupList {}
217#sub groupID {}
[119]218
219sub addUser {
220 my %args = @_;
221
[405]222 _commoncheck(\%args, 'y');
[119]223
[409]224# not sure how to usefully represent permissions via RPC. :/
[121]225# not to mention, permissions are checked at the UI layer, not the DB layer.
[119]226 # bend and twist; get those arguments in in the right order!
227 $args{type} = 'u' if !$args{type};
228 $args{permstring} = 'i' if !defined($args{permstring});
229 my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
230 for my $argname ('fname','lname','phone') {
231 last if !$args{$argname};
232 push @userargs, $args{$argname};
233 }
234 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
235 die $msg if $code eq 'FAIL';
236 return $msg;
237}
238
[405]239#sub getUserCount {}
240#sub getUserList {}
241#sub getUserDropdown {}
242#sub checkUser {}
[119]243
244sub updateUser {
245 my %args = @_;
246
[405]247 _commoncheck(\%args, 'y');
[119]248
[401]249 die "Missing UID\n" if !$args{uid};
[121]250
[119]251 # bend and twist; get those arguments in in the right order!
[411]252 $args{type} = 'u' if !$args{type};
[119]253 my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
254 for my $argname ('fname','lname','phone') {
255 last if !$args{$argname};
256 push @userargs, $args{$argname};
257 }
258##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
259# have to pass them all in to be overwritten
[411]260 my ($code,$msg) = DNSDB::updateUser($dbh, @userargs);
[119]261 die $msg if $code eq 'FAIL';
[412]262 return $msg;
[119]263}
264
265sub delUser {
266 my %args = @_;
267
[405]268 _commoncheck(\%args, 'y');
[119]269
[401]270 die "Missing UID\n" if !$args{uid};
[119]271 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
272 die $msg if $code eq 'FAIL';
[412]273 return $msg;
[119]274}
275
[405]276#sub userFullName {}
277#sub userStatus {}
278#sub getUserData {}
[119]279
[405]280#sub addLoc {}
281#sub updateLoc {}
282#sub delLoc {}
283#sub getLoc {}
284#sub getLocCount {}
285#sub getLocList {}
286
[447]287sub getLocDropdown {
288 my %args = @_;
289
290 _commoncheck(\%args);
291 $args{defloc} = '' if !$args{defloc};
292
293 my $ret = DNSDB::getLocDropdown($dbh, $args{group}, $args{defloc});
294 return $ret;
295}
296
[119]297sub getSOA {
298 my %args = @_;
299
[405]300 _commoncheck(\%args);
[121]301
[413]302 my $ret = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{id});
303 if (!$ret) {
304 if ($args{defrec} eq 'y') {
[401]305 die "No default SOA record in group\n";
[121]306 } else {
[413]307 die "No SOA record in zone\n";
[121]308 }
309 }
[413]310 return $ret;
[119]311}
312
[405]313#sub updateSOA {}
314
[119]315sub getRecLine {
316 my %args = @_;
317
[405]318 _commoncheck(\%args);
[123]319
[414]320 my $ret = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
[123]321
322 die $DNSDB::errstr if !$ret;
323
324 return $ret;
[119]325}
326
327sub getDomRecs {
328 my %args = @_;
329
[405]330 _commoncheck(\%args);
[123]331
[405]332 # set some optional args
[123]333 $args{nrecs} = 'all' if !$args{nrecs};
334 $args{nstart} = 0 if !$args{nstart};
335## for order, need to map input to column names
336 $args{order} = 'host' if !$args{order};
337 $args{direction} = 'ASC' if !$args{direction};
338
[401]339 my $ret = DNSDB::getDomRecs($dbh, (defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
340 offset => $args{offset}, sortby => $args{sortby}, sortorder => $args{sortorder},
341 filter => $args{filter}) );
[123]342
343 die $DNSDB::errstr if !$ret;
344
345 return $ret;
[119]346}
347
[123]348sub getRecCount {
349 my %args = @_;
[119]350
[405]351 _commoncheck(\%args);
[123]352
[405]353 # set some optional args
354 $args{nrecs} = 'all' if !$args{nrecs};
355 $args{nstart} = 0 if !$args{nstart};
356## for order, need to map input to column names
357 $args{order} = 'host' if !$args{order};
358 $args{direction} = 'ASC' if !$args{direction};
359
[416]360 my $ret = DNSDB::getRecCount($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{filter});
[405]361
362 die $DNSDB::errstr if !$ret;
363
364 return $ret;
[123]365}
366
[119]367sub addRec {
368 my %args = @_;
369
[405]370 _commoncheck(\%args, 'y');
[123]371
[426]372 # add records in the zone's default location if none is specified
373 if (!$args{location} && $args{defrec} eq 'n') {
374 $args{location} = DNSDB::getZoneLocation($dbh, $args{revrec}, $args{parent_id});
375 }
[123]376
[452]377 # callers may often not care about TTLs
378 if (!$args{ttl}) {
379 my $tmp = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{parent_id});
380 $args{ttl} = $tmp->{minttl};
381 }
382
[426]383 my @recargs = ($dbh, $args{defrec}, $args{revrec}, $args{parent_id},
384 \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location});
385 if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
386 push @recargs, $args{distance};
387 if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
388 push @recargs, $args{weight};
389 push @recargs, $args{port};
390 }
391 }
392
393 my ($code, $msg) = DNSDB::addRec(@recargs);
394
[123]395 die $msg if $code eq 'FAIL';
[426]396 return $msg;
[119]397}
398
399sub updateRec {
400 my %args = @_;
401
[405]402 _commoncheck(\%args, 'y');
[123]403
[452]404 # get old line, so we can update only the bits that the caller passed to change
405 # note we subbed address for val since it's a little more caller-friendly
406 my $oldrec = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
407 foreach my $field (qw(name type address ttl location distance weight port)) {
408 $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
409 }
410
[405]411 # note dist, weight, port are not required on all types; will be ignored if not needed.
[426]412 # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
413 my ($code, $msg) = DNSDB::updateRec($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
414 \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
415 $args{distance}, $args{weight}, $args{port});
[123]416
417 die $msg if $code eq 'FAIL';
[426]418 return $msg;
[119]419}
420
421sub delRec {
422 my %args = @_;
423
[405]424 _commoncheck(\%args, 'y');
[123]425
[426]426 my ($code, $msg) = DNSDB::delRec($dbh, $args{defrec}, $args{recrev}, $args{id});
[123]427
428 die $msg if $code eq 'FAIL';
[426]429 return $msg;
[119]430}
431
[405]432#sub getLogCount {}
433#sub getLogEntries {}
[452]434
435sub getRevPattern {
436 my %args = @_;
437
438 _commoncheck(\%args, 'y');
439
440 return DNSDB::getRevPattern($dbh, $args{cidr}, $args{group});
441}
442
[405]443#sub getTypelist {}
444#sub parentID {}
445#sub isParent {}
[123]446
[405]447sub zoneStatus {
[123]448 my %args = @_;
449
[405]450 _commoncheck(\%args, 'y');
[123]451
[405]452 my @arglist = ($dbh, $args{zoneid});
[123]453 push @arglist, $args{status} if defined($args{status});
454
[405]455 my $status = DNSDB::zoneStatus(@arglist);
[123]456}
457
[452]458# Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
459sub getZonesByCIDR {
460 my %args = @_;
461
462 _commoncheck(\%args, 'y');
463
464 return DNSDB::getZonesByCIDR($dbh, %args);
465}
466
[405]467#sub importAXFR {}
468#sub importBIND {}
469#sub import_tinydns {}
470#sub export {}
471#sub __export_tiny {}
472#sub _printrec_tiny {}
473#sub mailNotify {}
[119]474
475sub get_method_list {
476 my @methods = keys %{$methods};
477 return \@methods;
478}
Note: See TracBrowser for help on using the repository browser.