source: trunk/dns-rpc.cgi@ 196

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

/trunk

Minor fix in dns-rpc.cgi to load DB user/pass/etc with loadConfig

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