source: trunk/dns-rpc.cgi@ 122

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

/trunk

checkpoint
Flesh out getSOA() stub in dns-rpc.cgi
Tweak getSOA() in DNSDB.pm for better error handling

File size: 8.2 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
20# need to create a DNSDB object too
21my ($dbh,$msg) = DNSDB::connectDB("dnsdb","dnsdb","secret","dnsdbhost");
22DNSDB::initGlobals($dbh);
23
24my $methods = {
25 'dnsdb.addDomain' => \&addDomain,
26 'dnsdb.delDomain' => \&delDomain,
27 'dnsdb.addGroup' => \&addGroup,
28 'dnsdb.delGroup' => \&delGroup,
29 'dnsdb.addUser' => \&addUser,
30 'dnsdb.updateUser' => \&updateUser,
31 'dnsdb.delUser' => \&delUser,
32 'dnsdb.getSOA' => \&getSOA,
33
34 'dnsdb.getMethods' => \&get_method_list
35};
36
37my $res = Frontier::Responder->new(
38 methods => $methods
39 );
40
41# "Can't do that" errors
42##fixme: this MUST be loaded from a config file! Also must support multiple IPs
43if ($ENV{REMOTE_ADDR} ne '192.168.2.116') {
44 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, "Access denied");
45 exit;
46}
47if (!$dbh) {
48 print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
49 exit;
50}
51##fixme: fail on missing rpcuser/rpcsystem args
52
53print $res->answer;
54
55exit;
56
57##
58## Subs below here
59##
60
61#sub connectDB {
62#sub finish {
63#sub initGlobals {
64#sub initPermissions {
65#sub getPermissions {
66#sub changePermissions {
67#sub comparePermissions {
68#sub changeGroup {
69#sub _log {
70
71sub addDomain {
72 my %args = @_;
73
74 # Make sure we've got all the local bits we need
75 die "Missing remote username" if !$args{rpcuser}; # for logging
76 die "Missing remote system name" if !$args{rpcsystem}; # for logging
77
78 my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
79 die $msg if $code eq 'FAIL';
80 return $msg; # domain ID
81}
82
83sub delDomain {
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);
91 # Let's be nice; delete based on domid OR domain name. Saves an RPC call round-trip, maybe.
92 if ($args{domain} =~ /^\d+$/) {
93 ($code,$msg) = DNSDB::delDomain($dbh, $args{domain});
94 } else {
95 my $domid = DNSDB::domainID($dbh, $args{domain});
96 die "Can't find domain" if !$domid;
97 ($code,$msg) = DNSDB::delDomain($dbh, $domid);
98 }
99 die $msg if $code eq 'FAIL';
100}
101
102#sub domainName {
103#sub domainID {
104
105sub addGroup {
106 my %args = @_;
107
108 # Make sure we've got all the local bits we need
109 die "Missing remote username" if !$args{rpcuser}; # for logging
110 die "Missing remote system name" if !$args{rpcsystem}; # for logging
111
112# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
113# not to mention, permissions are checked at the UI layer, not the DB layer.
114 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
115 record_edit => 1, record_create => 1, record_delete => 1
116 };
117## optional $inhert arg?
118 my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
119 die $msg if $code eq 'FAIL';
120 return $msg;
121}
122
123sub delGroup {
124 my %args = @_;
125
126 # Make sure we've got all the local bits we need
127 die "Missing remote username" if !$args{rpcuser}; # for logging
128 die "Missing remote system name" if !$args{rpcsystem}; # for logging
129
130 my ($code,$msg);
131 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
132 if ($args{group} =~ /^\d+$/) {
133 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
134 } else {
135 my $grpid = DNSDB::groupID($dbh, $args{group});
136 die "Can't find group" if !$grpid;
137 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
138 }
139 die $msg if $code eq 'FAIL';
140}
141
142#sub getChildren {
143#sub groupName {
144#sub groupID {
145
146sub addUser {
147 my %args = @_;
148
149 # Make sure we've got all the local bits we need
150 die "Missing remote username" if !$args{rpcuser}; # for logging
151 die "Missing remote system name" if !$args{rpcsystem}; # for logging
152
153# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
154# not to mention, permissions are checked at the UI layer, not the DB layer.
155 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
156 record_edit => 1, record_create => 1, record_delete => 1
157 };
158 # bend and twist; get those arguments in in the right order!
159 $args{type} = 'u' if !$args{type};
160 $args{permstring} = 'i' if !defined($args{permstring});
161 my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
162 for my $argname ('fname','lname','phone') {
163 last if !$args{$argname};
164 push @userargs, $args{$argname};
165 }
166 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
167 die $msg if $code eq 'FAIL';
168 return $msg;
169}
170
171#sub checkUser {
172
173sub updateUser {
174 my %args = @_;
175
176 # Make sure we've got all the local bits we need
177 die "Missing remote username" if !$args{rpcuser}; # for logging
178 die "Missing remote system name" if !$args{rpcsystem}; # for logging
179
180 die "Missing UID" if !$args{uid};
181
182# not sure how to usefully represent permissions from any further out from DNSDB.pm :/
183# not to mention, permissions are checked at the UI layer, not the DB layer.
184 my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
185 record_edit => 1, record_create => 1, record_delete => 1
186 };
187 # bend and twist; get those arguments in in the right order!
188 my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
189 for my $argname ('fname','lname','phone') {
190 last if !$args{$argname};
191 push @userargs, $args{$argname};
192 }
193##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
194# have to pass them all in to be overwritten
195 my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
196 die $msg if $code eq 'FAIL';
197}
198
199sub delUser {
200 my %args = @_;
201
202 # Make sure we've got all the local bits we need
203 die "Missing remote username" if !$args{rpcuser}; # for logging
204 die "Missing remote system name" if !$args{rpcsystem}; # for logging
205
206 die "Missing UID" if !$args{uid};
207 my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
208 die $msg if $code eq 'FAIL';
209}
210
211#sub userFullName {
212#sub userStatus {
213#sub getUserData {
214
215sub getSOA {
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 my %ret = DNSDB::getSOA($dbh, $args{def}, $args{id});
223 if (!$ret{recid}) {
224 if ($args{def} eq 'y') {
225 die "No default SOA record in group";
226 } else {
227 die "No SOA record in domain";
228 }
229 }
230 return \%ret;
231}
232
233sub getRecLine {
234 my %args = @_;
235
236 # Make sure we've got all the local bits we need
237 die "Missing remote username" if !$args{rpcuser}; # for logging
238 die "Missing remote system name" if !$args{rpcsystem}; # for logging
239}
240
241sub getDomRecs {
242 my %args = @_;
243
244 # Make sure we've got all the local bits we need
245 die "Missing remote username" if !$args{rpcuser}; # for logging
246 die "Missing remote system name" if !$args{rpcsystem}; # for logging
247}
248
249#sub getRecCount {
250
251sub addRec {
252 my %args = @_;
253
254 # Make sure we've got all the local bits we need
255 die "Missing remote username" if !$args{rpcuser}; # for logging
256 die "Missing remote system name" if !$args{rpcsystem}; # for logging
257}
258
259sub updateRec {
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
267sub delRec {
268 my %args = @_;
269
270 # Make sure we've got all the local bits we need
271 die "Missing remote username" if !$args{rpcuser}; # for logging
272 die "Missing remote system name" if !$args{rpcsystem}; # for logging
273}
274
275#sub getParents {
276#sub domStatus {
277#sub importAXFR {
278#sub export {
279#sub __export_tiny {
280
281sub get_method_list {
282 my @methods = keys %{$methods};
283 return \@methods;
284}
Note: See TracBrowser for help on using the repository browser.