source: trunk/dns-rpc.cgi@ 405

Last change on this file since 405 was 405, checked in by Kris Deugau, 12 years ago

/trunk

Clean up some more annoyances in dns-rpc.cgi. See #43.

  • Update active and stub list of subs to match current DNSDB.pm
  • Factor out common opening errorcheck actions into a separate internal sub
  • Refresh getRecCount internals since it should be almost identical to getDomRecs
  • Fix a couple of trivial copypasted comment typos

Add support to handle adding and deleting reverse zones. See #26.

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