source: trunk/dns-rpc.cgi@ 229

Last change on this file since 229 was 216, checked in by Kris Deugau, 13 years ago

/trunk

Found some not-so-little things missing for a tarball

  • munging of @INC via "use lib" needs to be pointed to the right path on install
  • session dir should be configurable (eg, to put it in /var/lib/dnsdb when packaged scripts + templates all go in /usr/share/dnsdb)
  • Finally remove lingering unused viadns.css
  • Remember to install template files on 'make install'
  • Get a good start on filling out INSTALL halfway properly by copy-pasting the IPDB version
  • Add all-commented example config file to be installed
File size: 10.5 KB
RevLine 
[216]1#!/usr/bin/perl -w -T
[200]2# $Id$
[119]3# XMLRPC interface to manipulate most DNS DB entities
[200]4# Copyright (C) 2011 - Kris Deugau <kdeugau@deepnet.cx>
[119]5
6use strict;
7use warnings;
[216]8
9# don't remove! required for GNU/FHS-ish install from tarball
10use lib '.'; ##uselib##
11
[119]12use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
[121]13use Data::Dumper;
[119]14
15#use Frontier::RPC2;
16use Frontier::Responder;
17
18## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
19## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
20## that needs kicking
21#### hmm. put this in a separate file?
22#package DNSDB::RPC;
23#our @ISA = ("Frontier::RPC2", "Frontier::Responder");
24#package main;
25
[191]26loadConfig();
27
[119]28# need to create a DNSDB object too
[191]29my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
30 $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
31
[121]32DNSDB::initGlobals($dbh);
[119]33
34my $methods = {
35 'dnsdb.addDomain' => \&addDomain,
36 'dnsdb.delDomain' => \&delDomain,
[121]37 'dnsdb.addGroup' => \&addGroup,
38 'dnsdb.delGroup' => \&delGroup,
39 'dnsdb.addUser' => \&addUser,
40 'dnsdb.updateUser' => \&updateUser,
41 'dnsdb.delUser' => \&delUser,
42 'dnsdb.getSOA' => \&getSOA,
[123]43 'dnsdb.getRecLine' => \&getRecLine,
44 'dnsdb.getDomRecs' => \&getDomRecs,
45 'dnsdb.getRecCount' => \&getRecCount,
46 'dnsdb.addRec' => \&addRec,
47 'dnsdb.delRec' => \&delRec,
48 'dnsdb.domStatus' => \&domStatus,
[121]49
[119]50 'dnsdb.getMethods' => \&get_method_list
51};
52
53my $res = Frontier::Responder->new(
54 methods => $methods
55 );
56
57# "Can't do that" errors
58##fixme: this MUST be loaded from a config file! Also must support multiple IPs
59if ($ENV{REMOTE_ADDR} ne '192.168.2.116') {
60 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, "Access denied");
61 exit;
62}
63if (!$dbh) {
64 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
65 exit;
66}
67##fixme: fail on missing rpcuser/rpcsystem args
68
69print $res->answer;
70
71exit;
72
73##
74## Subs below here
75##
76
77#sub connectDB {
78#sub finish {
79#sub initGlobals {
80#sub initPermissions {
81#sub getPermissions {
82#sub changePermissions {
83#sub comparePermissions {
84#sub changeGroup {
85#sub _log {
86
87sub addDomain {
88 my %args = @_;
89
90 # Make sure we've got all the local bits we need
91 die "Missing remote username" if !$args{rpcuser}; # for logging
92 die "Missing remote system name" if !$args{rpcsystem}; # for logging
93
94 my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
95 die $msg if $code eq 'FAIL';
96 return $msg; # domain ID
97}
98
99sub delDomain {
100 my %args = @_;
101
102 # Make sure we've got all the local bits we need
103 die "Missing remote username" if !$args{rpcuser}; # for logging
104 die "Missing remote system name" if !$args{rpcsystem}; # for logging
105
[121]106 my ($code,$msg);
[119]107 # Let's be nice; delete based on domid OR domain name. Saves an RPC call round-trip, maybe.
108 if ($args{domain} =~ /^\d+$/) {
[121]109 ($code,$msg) = DNSDB::delDomain($dbh, $args{domain});
[119]110 } else {
111 my $domid = DNSDB::domainID($dbh, $args{domain});
112 die "Can't find domain" if !$domid;
[121]113 ($code,$msg) = DNSDB::delDomain($dbh, $domid);
[119]114 }
115 die $msg if $code eq 'FAIL';
116}
117
118#sub domainName {
119#sub domainID {
120
121sub addGroup {
122 my %args = @_;
123
124 # Make sure we've got all the local bits we need
125 die "Missing remote username" if !$args{rpcuser}; # for logging
126 die "Missing remote system name" if !$args{rpcsystem}; # for logging
127
128# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
[121]129# not to mention, permissions are checked at the UI layer, not the DB layer.
[119]130 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
131 record_edit => 1, record_create => 1, record_delete => 1
132 };
133## optional $inhert arg?
134 my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
135 die $msg if $code eq 'FAIL';
136 return $msg;
137}
138
139sub delGroup {
140 my %args = @_;
141
142 # Make sure we've got all the local bits we need
143 die "Missing remote username" if !$args{rpcuser}; # for logging
144 die "Missing remote system name" if !$args{rpcsystem}; # for logging
145
[121]146 my ($code,$msg);
[119]147 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
148 if ($args{group} =~ /^\d+$/) {
[121]149 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
[119]150 } else {
151 my $grpid = DNSDB::groupID($dbh, $args{group});
152 die "Can't find group" if !$grpid;
[121]153 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
[119]154 }
155 die $msg if $code eq 'FAIL';
156}
157
158#sub getChildren {
159#sub groupName {
160#sub groupID {
161
162sub addUser {
163 my %args = @_;
164
165 # Make sure we've got all the local bits we need
166 die "Missing remote username" if !$args{rpcuser}; # for logging
167 die "Missing remote system name" if !$args{rpcsystem}; # for logging
168
[121]169# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
170# not to mention, permissions are checked at the UI layer, not the DB layer.
171 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
172 record_edit => 1, record_create => 1, record_delete => 1
173 };
[119]174 # bend and twist; get those arguments in in the right order!
175 $args{type} = 'u' if !$args{type};
176 $args{permstring} = 'i' if !defined($args{permstring});
177 my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
178 for my $argname ('fname','lname','phone') {
179 last if !$args{$argname};
180 push @userargs, $args{$argname};
181 }
182 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
183 die $msg if $code eq 'FAIL';
184 return $msg;
185}
186
187#sub checkUser {
188
189sub updateUser {
190 my %args = @_;
191
192 # Make sure we've got all the local bits we need
193 die "Missing remote username" if !$args{rpcuser}; # for logging
194 die "Missing remote system name" if !$args{rpcsystem}; # for logging
195
196 die "Missing UID" if !$args{uid};
[121]197
198# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
199# not to mention, permissions are checked at the UI layer, not the DB layer.
200 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
201 record_edit => 1, record_create => 1, record_delete => 1
202 };
[119]203 # bend and twist; get those arguments in in the right order!
204 my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
205 for my $argname ('fname','lname','phone') {
206 last if !$args{$argname};
207 push @userargs, $args{$argname};
208 }
209##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
210# have to pass them all in to be overwritten
211 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
212 die $msg if $code eq 'FAIL';
213}
214
215sub delUser {
216 my %args = @_;
217
218 # Make sure we've got all the local bits we need
219 die "Missing remote username" if !$args{rpcuser}; # for logging
220 die "Missing remote system name" if !$args{rpcsystem}; # for logging
221
222 die "Missing UID" if !$args{uid};
223 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
224 die $msg if $code eq 'FAIL';
225}
226
227#sub userFullName {
228#sub userStatus {
229#sub getUserData {
230
231sub getSOA {
232 my %args = @_;
233
234 # Make sure we've got all the local bits we need
235 die "Missing remote username" if !$args{rpcuser}; # for logging
236 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[121]237
238 my %ret = DNSDB::getSOA($dbh, $args{def}, $args{id});
239 if (!$ret{recid}) {
240 if ($args{def} eq 'y') {
241 die "No default SOA record in group";
242 } else {
243 die "No SOA record in domain";
244 }
245 }
246 return \%ret;
[119]247}
248
249sub getRecLine {
250 my %args = @_;
251
252 # Make sure we've got all the local bits we need
253 die "Missing remote username" if !$args{rpcuser}; # for logging
254 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[123]255
256 my $ret = DNSDB::getRecLine($dbh, $args{def}, $args{id});
257
258 die $DNSDB::errstr if !$ret;
259
260 return $ret;
[119]261}
262
263sub getDomRecs {
264 my %args = @_;
265
266 # Make sure we've got all the local bits we need
267 die "Missing remote username" if !$args{rpcuser}; # for logging
268 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[123]269
270#bleh
271 $args{nrecs} = 'all' if !$args{nrecs};
272 $args{nstart} = 0 if !$args{nstart};
273## for order, need to map input to column names
274 $args{order} = 'host' if !$args{order};
275 $args{direction} = 'ASC' if !$args{direction};
276
277 my $ret = DNSDB::getDomRecs($dbh, $args{def}, $args{id}, $args{nrecs}, $args{nstart}, $args{order}, $args{direction});
278
279 die $DNSDB::errstr if !$ret;
280
281 return $ret;
[119]282}
283
[123]284sub getRecCount {
285 my %args = @_;
[119]286
[123]287 # Make sure we've got all the local bits we need
288 die "Missing remote username" if !$args{rpcuser}; # for logging
289 die "Missing remote system name" if !$args{rpcsystem}; # for logging
290
291 return DNSDB::getRecCount($dbh, $id);
292}
293
[119]294sub addRec {
295 my %args = @_;
296
297 # Make sure we've got all the local bits we need
298 die "Missing remote username" if !$args{rpcuser}; # for logging
299 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[123]300
301 # note dist, weight, port are not reequired on all types; will be ignored if not needed.
302 my ($code, $msg) = DNSDB::addRec($dbh, $args{def}, $args{domid}, $args{host}, $typemap{$args{type}},
303 $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
304
305 die $msg if $code eq 'FAIL';
[119]306}
307
308sub updateRec {
309 my %args = @_;
310
311 # Make sure we've got all the local bits we need
312 die "Missing remote username" if !$args{rpcuser}; # for logging
313 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[123]314
315 # note dist, weight, port are not reequired on all types; will be ignored if not needed.
316 my ($code, $msg) = DNSDB::updateRec($dbh, $args{def}, $args{recid}, $args{host}, $typemap{$args{type}},
317 $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
318
319 die $msg if $code eq 'FAIL';
[119]320}
321
322sub delRec {
323 my %args = @_;
324
325 # Make sure we've got all the local bits we need
326 die "Missing remote username" if !$args{rpcuser}; # for logging
327 die "Missing remote system name" if !$args{rpcsystem}; # for logging
[123]328
329 # note dist, weight, port are not reequired on all types; will be ignored if not needed.
330 my ($code, $msg) = DNSDB::delRec($dbh, $args{def}, $args{recid});
331
332 die $msg if $code eq 'FAIL';
[119]333}
334
335#sub getParents {
[123]336
337sub domStatus {
338 my %args = @_;
339
340 # Make sure we've got all the local bits we need
341 die "Missing remote username" if !$args{rpcuser}; # for logging
342 die "Missing remote system name" if !$args{rpcsystem}; # for logging
343
344 my @arglist = ($dbh, $args{domid});
345 push @arglist, $args{status} if defined($args{status});
346
347 my $status = DNSDB::domStatus(@arglist);
348}
349
[119]350#sub importAXFR {
351#sub export {
352#sub __export_tiny {
353
354sub get_method_list {
355 my @methods = keys %{$methods};
356 return \@methods;
357}
Note: See TracBrowser for help on using the repository browser.