source: trunk/dns-rpc.cgi@ 216

Last change on this file since 216 was 216, checked in by Kris Deugau, 12 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
Line 
1#!/usr/bin/perl -w -T
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;
8
9# don't remove! required for GNU/FHS-ish install from tarball
10use lib '.'; ##uselib##
11
12use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
13use Data::Dumper;
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
26loadConfig();
27
28# need to create a DNSDB object too
29my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
30 $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
31
32DNSDB::initGlobals($dbh);
33
34my $methods = {
35 'dnsdb.addDomain' => \&addDomain,
36 'dnsdb.delDomain' => \&delDomain,
37 'dnsdb.addGroup' => \&addGroup,
38 'dnsdb.delGroup' => \&delGroup,
39 'dnsdb.addUser' => \&addUser,
40 'dnsdb.updateUser' => \&updateUser,
41 'dnsdb.delUser' => \&delUser,
42 'dnsdb.getSOA' => \&getSOA,
43 'dnsdb.getRecLine' => \&getRecLine,
44 'dnsdb.getDomRecs' => \&getDomRecs,
45 'dnsdb.getRecCount' => \&getRecCount,
46 'dnsdb.addRec' => \&addRec,
47 'dnsdb.delRec' => \&delRec,
48 'dnsdb.domStatus' => \&domStatus,
49
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
106 my ($code,$msg);
107 # Let's be nice; delete based on domid OR domain name. Saves an RPC call round-trip, maybe.
108 if ($args{domain} =~ /^\d+$/) {
109 ($code,$msg) = DNSDB::delDomain($dbh, $args{domain});
110 } else {
111 my $domid = DNSDB::domainID($dbh, $args{domain});
112 die "Can't find domain" if !$domid;
113 ($code,$msg) = DNSDB::delDomain($dbh, $domid);
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 :/
129# not to mention, permissions are checked at the UI layer, not the DB layer.
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
146 my ($code,$msg);
147 # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
148 if ($args{group} =~ /^\d+$/) {
149 ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
150 } else {
151 my $grpid = DNSDB::groupID($dbh, $args{group});
152 die "Can't find group" if !$grpid;
153 ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
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
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 };
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};
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 };
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
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;
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
255
256 my $ret = DNSDB::getRecLine($dbh, $args{def}, $args{id});
257
258 die $DNSDB::errstr if !$ret;
259
260 return $ret;
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
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;
282}
283
284sub getRecCount {
285 my %args = @_;
286
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
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
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';
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
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';
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
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';
333}
334
335#sub getParents {
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
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.