source: trunk/dns-rpc.cgi@ 447

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

/trunk

Tweak RPC handler for updated addRDNS().
Add and fill in getLocDropdown() in RPC handler so that callers
can retrieve a list of locations to pass back when adding or changing
records and reverse zones.

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