source: trunk/dns-rpc.cgi@ 215

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

/trunk

Nitpickish file header tweaks

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