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
Line 
1#!/usr/bin/perl -w -T
2# XMLRPC interface to manipulate most DNS DB entities
3##
4# $Id: dns-rpc.cgi 452 2013-01-11 17:21:13Z 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.delRec' => \&delRec,
66#sub getLogCount {}
67#sub getLogEntries {}
68 'dnsdb.getRevPattern' => \&getRevPattern,
69 'dnsdb.zoneStatus' => \&zoneStatus,
70 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
71
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
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
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
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
129 _commoncheck(\%args, 'y');
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
136sub delZone {
137 my %args = @_;
138
139 _commoncheck(\%args, 'y');
140 die "Need forward/reverse zone flag\n" if !$args{revrec};
141
142 my ($code,$msg);
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});
146 } else {
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});
152 }
153 die $msg if $code eq 'FAIL';
154 return $msg;
155}
156
157#sub domainName {}
158#sub revName {}
159#sub domainID {}
160#sub revID {}
161
162sub addRDNS {
163 my %args = @_;
164
165 _commoncheck(\%args, 'y');
166
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';
169 return $msg; # domain ID
170}
171
172#sub getZoneCount {}
173#sub getZoneList {}
174#sub getZoneLocation {}
175
176sub addGroup {
177 my %args = @_;
178
179 _commoncheck(\%args, 'y');
180 die "Missing new group name\n" if !$args{groupname};
181 die "Missing parent group ID\n" if !$args{parent_id};
182
183# not sure how to usefully represent permissions via RPC. :/
184# not to mention, permissions are checked at the UI layer, not the DB layer.
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
197 _commoncheck(\%args, 'y');
198 die "Missing group ID or name to remove\n" if !$args{group};
199
200 my ($code,$msg);
201 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
202 if ($args{group} =~ /^\d+$/) {
203 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
204 } else {
205 my $grpid = DNSDB::groupID($dbh, $args{group});
206 die "Can't find group\n" if !$grpid;
207 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
208 }
209 die $msg if $code eq 'FAIL';
210 return $msg;
211}
212
213#sub getChildren {}
214#sub groupName {}
215#sub getGroupCount {}
216#sub getGroupList {}
217#sub groupID {}
218
219sub addUser {
220 my %args = @_;
221
222 _commoncheck(\%args, 'y');
223
224# not sure how to usefully represent permissions via RPC. :/
225# not to mention, permissions are checked at the UI layer, not the DB layer.
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
239#sub getUserCount {}
240#sub getUserList {}
241#sub getUserDropdown {}
242#sub checkUser {}
243
244sub updateUser {
245 my %args = @_;
246
247 _commoncheck(\%args, 'y');
248
249 die "Missing UID\n" if !$args{uid};
250
251 # bend and twist; get those arguments in in the right order!
252 $args{type} = 'u' if !$args{type};
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
260 my ($code,$msg) = DNSDB::updateUser($dbh, @userargs);
261 die $msg if $code eq 'FAIL';
262 return $msg;
263}
264
265sub delUser {
266 my %args = @_;
267
268 _commoncheck(\%args, 'y');
269
270 die "Missing UID\n" if !$args{uid};
271 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
272 die $msg if $code eq 'FAIL';
273 return $msg;
274}
275
276#sub userFullName {}
277#sub userStatus {}
278#sub getUserData {}
279
280#sub addLoc {}
281#sub updateLoc {}
282#sub delLoc {}
283#sub getLoc {}
284#sub getLocCount {}
285#sub getLocList {}
286
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
297sub getSOA {
298 my %args = @_;
299
300 _commoncheck(\%args);
301
302 my $ret = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{id});
303 if (!$ret) {
304 if ($args{defrec} eq 'y') {
305 die "No default SOA record in group\n";
306 } else {
307 die "No SOA record in zone\n";
308 }
309 }
310 return $ret;
311}
312
313#sub updateSOA {}
314
315sub getRecLine {
316 my %args = @_;
317
318 _commoncheck(\%args);
319
320 my $ret = DNSDB::getRecLine($dbh, $args{defrec}, $args{revrec}, $args{id});
321
322 die $DNSDB::errstr if !$ret;
323
324 return $ret;
325}
326
327sub getDomRecs {
328 my %args = @_;
329
330 _commoncheck(\%args);
331
332 # set some optional args
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
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}) );
342
343 die $DNSDB::errstr if !$ret;
344
345 return $ret;
346}
347
348sub getRecCount {
349 my %args = @_;
350
351 _commoncheck(\%args);
352
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
360 my $ret = DNSDB::getRecCount($dbh, $args{defrec}, $args{revrec}, $args{id}, $args{filter});
361
362 die $DNSDB::errstr if !$ret;
363
364 return $ret;
365}
366
367sub addRec {
368 my %args = @_;
369
370 _commoncheck(\%args, 'y');
371
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 }
376
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
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
395 die $msg if $code eq 'FAIL';
396 return $msg;
397}
398
399sub updateRec {
400 my %args = @_;
401
402 _commoncheck(\%args, 'y');
403
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
411 # note dist, weight, port are not required on all types; will be ignored if not needed.
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});
416
417 die $msg if $code eq 'FAIL';
418 return $msg;
419}
420
421sub delRec {
422 my %args = @_;
423
424 _commoncheck(\%args, 'y');
425
426 my ($code, $msg) = DNSDB::delRec($dbh, $args{defrec}, $args{recrev}, $args{id});
427
428 die $msg if $code eq 'FAIL';
429 return $msg;
430}
431
432#sub getLogCount {}
433#sub getLogEntries {}
434
435sub getRevPattern {
436 my %args = @_;
437
438 _commoncheck(\%args, 'y');
439
440 return DNSDB::getRevPattern($dbh, $args{cidr}, $args{group});
441}
442
443#sub getTypelist {}
444#sub parentID {}
445#sub isParent {}
446
447sub zoneStatus {
448 my %args = @_;
449
450 _commoncheck(\%args, 'y');
451
452 my @arglist = ($dbh, $args{zoneid});
453 push @arglist, $args{status} if defined($args{status});
454
455 my $status = DNSDB::zoneStatus(@arglist);
456}
457
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
467#sub importAXFR {}
468#sub importBIND {}
469#sub import_tinydns {}
470#sub export {}
471#sub __export_tiny {}
472#sub _printrec_tiny {}
473#sub mailNotify {}
474
475sub get_method_list {
476 my @methods = keys %{$methods};
477 return \@methods;
478}
Note: See TracBrowser for help on using the repository browser.