1 | #!/usr/bin/perl -w -T
|
---|
2 | # XMLRPC interface to manipulate most DNS DB entities
|
---|
3 | ##
|
---|
4 | # $Id: dns-rpc.cgi 409 2012-10-05 17:42:58Z kdeugau $
|
---|
5 | # Copyright 2012 Kris Deugau <kdeugau@deepnet.cx>
|
---|
6 | #
|
---|
7 | # This program is free software: you can redistribute it and/or modify
|
---|
8 | # it under the terms of the GNU General Public License as published by
|
---|
9 | # the Free Software Foundation, either version 3 of the License, or
|
---|
10 | # (at your option) any later version.
|
---|
11 | #
|
---|
12 | # This program is distributed in the hope that it will be useful,
|
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | # GNU General Public License for more details.
|
---|
16 | #
|
---|
17 | # You should have received a copy of the GNU General Public License
|
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ##
|
---|
20 |
|
---|
21 | use strict;
|
---|
22 | use warnings;
|
---|
23 |
|
---|
24 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
25 | use lib '.'; ##uselib##
|
---|
26 |
|
---|
27 | use DNSDB; # note we're not importing subs; this lets us (ab)use the same sub names here for convenience
|
---|
28 | use Data::Dumper;
|
---|
29 |
|
---|
30 | #use Frontier::RPC2;
|
---|
31 | use Frontier::Responder;
|
---|
32 |
|
---|
33 | ## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
|
---|
34 | ## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
|
---|
35 | ## that needs kicking
|
---|
36 | #### hmm. put this in a separate file?
|
---|
37 | #package DNSDB::RPC;
|
---|
38 | #our @ISA = ("Frontier::RPC2", "Frontier::Responder");
|
---|
39 | #package main;
|
---|
40 |
|
---|
41 | DNSDB::loadConfig(rpcflag => 1);
|
---|
42 |
|
---|
43 | # need to create a DNSDB object too
|
---|
44 | my ($dbh,$msg) = DNSDB::connectDB($DNSDB::config{dbname}, $DNSDB::config{dbuser},
|
---|
45 | $DNSDB::config{dbpass}, $DNSDB::config{dbhost});
|
---|
46 |
|
---|
47 | DNSDB::initGlobals($dbh);
|
---|
48 |
|
---|
49 | my $methods = {
|
---|
50 | 'dnsdb.addDomain' => \&addDomain,
|
---|
51 | 'dnsdb.delZone' => \&delZone,
|
---|
52 | 'dnsdb.addRDNS' => \&addRDNS,
|
---|
53 | 'dnsdb.addGroup' => \&addGroup,
|
---|
54 | 'dnsdb.delGroup' => \&delGroup,
|
---|
55 | 'dnsdb.addUser' => \&addUser,
|
---|
56 | 'dnsdb.updateUser' => \&updateUser,
|
---|
57 | 'dnsdb.delUser' => \&delUser,
|
---|
58 | 'dnsdb.getSOA' => \&getSOA,
|
---|
59 | 'dnsdb.getRecLine' => \&getRecLine,
|
---|
60 | 'dnsdb.getDomRecs' => \&getDomRecs,
|
---|
61 | 'dnsdb.getRecCount' => \&getRecCount,
|
---|
62 | 'dnsdb.addRec' => \&addRec,
|
---|
63 | 'dnsdb.updateRec' => \&updateRec,
|
---|
64 | 'dnsdb.delRec' => \&delRec,
|
---|
65 | 'dnsdb.zoneStatus' => \&zoneStatus,
|
---|
66 |
|
---|
67 | 'dnsdb.getMethods' => \&get_method_list
|
---|
68 | };
|
---|
69 |
|
---|
70 | my $res = Frontier::Responder->new(
|
---|
71 | methods => $methods
|
---|
72 | );
|
---|
73 |
|
---|
74 | # "Can't do that" errors
|
---|
75 | if (!$dbh) {
|
---|
76 | print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $msg);
|
---|
77 | exit;
|
---|
78 | }
|
---|
79 | ##fixme: fail on missing rpcuser/rpcsystem args
|
---|
80 |
|
---|
81 | print $res->answer;
|
---|
82 |
|
---|
83 | exit;
|
---|
84 |
|
---|
85 | ##
|
---|
86 | ## Subs below here
|
---|
87 | ##
|
---|
88 |
|
---|
89 | # Utility subs
|
---|
90 | sub _aclcheck {
|
---|
91 | my $subsys = shift;
|
---|
92 | return 1 if grep /$ENV{REMOTE_ADDR}/, @{$DNSDB::config{rpcacl}{$subsys}};
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | # Let's see if we can factor these out of the RPC method subs
|
---|
97 | sub _commoncheck {
|
---|
98 | my $argref = shift;
|
---|
99 | my $needslog = shift;
|
---|
100 |
|
---|
101 | die "Missing remote system name\n" if !$argref->{rpcsystem};
|
---|
102 | die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
|
---|
103 | if ($needslog) {
|
---|
104 | die "Missing remote username\n" if !$argref->{rpcuser};
|
---|
105 | die "Couldn't set userdata for logging\n"
|
---|
106 | unless DNSDB::initRPC($dbh, (username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
|
---|
107 | fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) ) );
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | #sub connectDB {
|
---|
112 | #sub finish {
|
---|
113 | #sub initGlobals {
|
---|
114 | #sub initPermissions {
|
---|
115 | #sub getPermissions {
|
---|
116 | #sub changePermissions {
|
---|
117 | #sub comparePermissions {
|
---|
118 | #sub changeGroup {
|
---|
119 | #sub _log {
|
---|
120 |
|
---|
121 | sub addDomain {
|
---|
122 | my %args = @_;
|
---|
123 |
|
---|
124 | _commoncheck(\%args, 'y');
|
---|
125 |
|
---|
126 | my ($code, $msg) = DNSDB::addDomain($dbh, $args{domain}, $args{group}, $args{state});
|
---|
127 | die $msg if $code eq 'FAIL';
|
---|
128 | return $msg; # domain ID
|
---|
129 | }
|
---|
130 |
|
---|
131 | sub delZone {
|
---|
132 | my %args = @_;
|
---|
133 |
|
---|
134 | _commoncheck(\%args, 'y');
|
---|
135 | die "Need forward/reverse zone flag\n" if !$args{revrec};
|
---|
136 |
|
---|
137 | my ($code,$msg);
|
---|
138 | # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
|
---|
139 | if ($args{zone} =~ /^\d+$/) {
|
---|
140 | ($code,$msg) = DNSDB::delZone($dbh, $args{zone}, $args{revrec});
|
---|
141 | } else {
|
---|
142 | my $zoneid;
|
---|
143 | $zoneid = DNSDB::domainID($dbh, $args{zone}) if $args{revrec} eq 'n';
|
---|
144 | $zoneid = DNSDB::revID($dbh, $args{zone}) if $args{revrec} eq 'y';
|
---|
145 | die "Can't find zone: $DNSDB::errstr\n" if !$zoneid;
|
---|
146 | ($code,$msg) = DNSDB::delZone($dbh, $zoneid, $args{revrec});
|
---|
147 | }
|
---|
148 | die $msg if $code eq 'FAIL';
|
---|
149 | return $msg;
|
---|
150 | }
|
---|
151 |
|
---|
152 | #sub domainName {}
|
---|
153 | #sub revName {}
|
---|
154 | #sub domainID {}
|
---|
155 | #sub revID {}
|
---|
156 |
|
---|
157 | sub addRDNS {
|
---|
158 | my %args = @_;
|
---|
159 |
|
---|
160 | _commoncheck(\%args, 'y');
|
---|
161 |
|
---|
162 | my ($code, $msg) = DNSDB::addRDNS($dbh, $args{revzone}, $args{revpatt}, $args{group}, $args{state});
|
---|
163 | die $msg if $code eq 'FAIL';
|
---|
164 | return $msg; # domain ID
|
---|
165 | }
|
---|
166 |
|
---|
167 | #sub getZoneCount {}
|
---|
168 | #sub getZoneList {}
|
---|
169 | #sub getZoneLocation {}
|
---|
170 |
|
---|
171 | sub addGroup {
|
---|
172 | my %args = @_;
|
---|
173 |
|
---|
174 | _commoncheck(\%args, 'y');
|
---|
175 | die "Missing new group name\n" if !$args{groupname};
|
---|
176 | die "Missing parent group ID\n" if !$args{parent_id};
|
---|
177 |
|
---|
178 | # not sure how to usefully represent permissions via RPC. :/
|
---|
179 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
180 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
181 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
182 | };
|
---|
183 | ## optional $inhert arg?
|
---|
184 | my ($code,$msg) = DNSDB::addGroup($dbh, $args{groupname}, $args{parent_id}, $perms);
|
---|
185 | die $msg if $code eq 'FAIL';
|
---|
186 | return $msg;
|
---|
187 | }
|
---|
188 |
|
---|
189 | sub delGroup {
|
---|
190 | my %args = @_;
|
---|
191 |
|
---|
192 | _commoncheck(\%args, 'y');
|
---|
193 | die "Missing group ID or name to remove\n" if !$args{group};
|
---|
194 |
|
---|
195 | my ($code,$msg);
|
---|
196 | # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
|
---|
197 | if ($args{group} =~ /^\d+$/) {
|
---|
198 | ($code,$msg) = DNSDB::delGroup($dbh, $args{group});
|
---|
199 | } else {
|
---|
200 | my $grpid = DNSDB::groupID($dbh, $args{group});
|
---|
201 | die "Can't find group\n" if !$grpid;
|
---|
202 | ($code,$msg) = DNSDB::delGroup($dbh, $grpid);
|
---|
203 | }
|
---|
204 | die $msg if $code eq 'FAIL';
|
---|
205 | return $msg;
|
---|
206 | }
|
---|
207 |
|
---|
208 | #sub getChildren {}
|
---|
209 | #sub groupName {}
|
---|
210 | #sub getGroupCount {}
|
---|
211 | #sub getGroupList {}
|
---|
212 | #sub groupID {}
|
---|
213 |
|
---|
214 | sub addUser {
|
---|
215 | my %args = @_;
|
---|
216 |
|
---|
217 | _commoncheck(\%args, 'y');
|
---|
218 |
|
---|
219 | # not sure how to usefully represent permissions via RPC. :/
|
---|
220 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
221 | # bend and twist; get those arguments in in the right order!
|
---|
222 | $args{type} = 'u' if !$args{type};
|
---|
223 | $args{permstring} = 'i' if !defined($args{permstring});
|
---|
224 | my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
|
---|
225 | for my $argname ('fname','lname','phone') {
|
---|
226 | last if !$args{$argname};
|
---|
227 | push @userargs, $args{$argname};
|
---|
228 | }
|
---|
229 | my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
|
---|
230 | die $msg if $code eq 'FAIL';
|
---|
231 | return $msg;
|
---|
232 | }
|
---|
233 |
|
---|
234 | #sub getUserCount {}
|
---|
235 | #sub getUserList {}
|
---|
236 | #sub getUserDropdown {}
|
---|
237 | #sub checkUser {}
|
---|
238 |
|
---|
239 | sub updateUser {
|
---|
240 | my %args = @_;
|
---|
241 |
|
---|
242 | _commoncheck(\%args, 'y');
|
---|
243 |
|
---|
244 | die "Missing UID\n" if !$args{uid};
|
---|
245 |
|
---|
246 | # not sure how to usefully represent permissions from any further out from DNSDB.pm :/
|
---|
247 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
248 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
249 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
250 | };
|
---|
251 | # bend and twist; get those arguments in in the right order!
|
---|
252 | my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
|
---|
253 | for my $argname ('fname','lname','phone') {
|
---|
254 | last if !$args{$argname};
|
---|
255 | push @userargs, $args{$argname};
|
---|
256 | }
|
---|
257 | ##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
|
---|
258 | # have to pass them all in to be overwritten
|
---|
259 | my ($code,$msg) = DNSDB::addUser($dbh, @userargs);
|
---|
260 | die $msg if $code eq 'FAIL';
|
---|
261 | }
|
---|
262 |
|
---|
263 | sub delUser {
|
---|
264 | my %args = @_;
|
---|
265 |
|
---|
266 | _commoncheck(\%args, 'y');
|
---|
267 |
|
---|
268 | die "Missing UID\n" if !$args{uid};
|
---|
269 | my ($code,$msg) = DNSDB::delUser($dbh, $args{uid});
|
---|
270 | die $msg if $code eq 'FAIL';
|
---|
271 | }
|
---|
272 |
|
---|
273 | #sub userFullName {}
|
---|
274 | #sub userStatus {}
|
---|
275 | #sub getUserData {}
|
---|
276 |
|
---|
277 | #sub addLoc {}
|
---|
278 | #sub updateLoc {}
|
---|
279 | #sub delLoc {}
|
---|
280 | #sub getLoc {}
|
---|
281 | #sub getLocCount {}
|
---|
282 | #sub getLocList {}
|
---|
283 | #sub getLocDropdown {}
|
---|
284 |
|
---|
285 | sub getSOA {
|
---|
286 | my %args = @_;
|
---|
287 |
|
---|
288 | _commoncheck(\%args);
|
---|
289 |
|
---|
290 | my %ret = DNSDB::getSOA($dbh, $args{def}, $args{id});
|
---|
291 | if (!$ret{recid}) {
|
---|
292 | if ($args{def} eq 'y') {
|
---|
293 | die "No default SOA record in group\n";
|
---|
294 | } else {
|
---|
295 | die "No SOA record in domain\n";
|
---|
296 | }
|
---|
297 | }
|
---|
298 | return \%ret;
|
---|
299 | }
|
---|
300 |
|
---|
301 | #sub updateSOA {}
|
---|
302 |
|
---|
303 | sub getRecLine {
|
---|
304 | my %args = @_;
|
---|
305 |
|
---|
306 | _commoncheck(\%args);
|
---|
307 |
|
---|
308 | my $ret = DNSDB::getRecLine($dbh, $args{def}, $args{id});
|
---|
309 |
|
---|
310 | die $DNSDB::errstr if !$ret;
|
---|
311 |
|
---|
312 | return $ret;
|
---|
313 | }
|
---|
314 |
|
---|
315 | sub getDomRecs {
|
---|
316 | my %args = @_;
|
---|
317 |
|
---|
318 | _commoncheck(\%args);
|
---|
319 |
|
---|
320 | # set some optional args
|
---|
321 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
322 | $args{nstart} = 0 if !$args{nstart};
|
---|
323 | ## for order, need to map input to column names
|
---|
324 | $args{order} = 'host' if !$args{order};
|
---|
325 | $args{direction} = 'ASC' if !$args{direction};
|
---|
326 |
|
---|
327 | my $ret = DNSDB::getDomRecs($dbh, (defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
|
---|
328 | offset => $args{offset}, sortby => $args{sortby}, sortorder => $args{sortorder},
|
---|
329 | filter => $args{filter}) );
|
---|
330 |
|
---|
331 | die $DNSDB::errstr if !$ret;
|
---|
332 |
|
---|
333 | return $ret;
|
---|
334 | }
|
---|
335 |
|
---|
336 | sub getRecCount {
|
---|
337 | my %args = @_;
|
---|
338 |
|
---|
339 | _commoncheck(\%args);
|
---|
340 |
|
---|
341 | # set some optional args
|
---|
342 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
343 | $args{nstart} = 0 if !$args{nstart};
|
---|
344 | ## for order, need to map input to column names
|
---|
345 | $args{order} = 'host' if !$args{order};
|
---|
346 | $args{direction} = 'ASC' if !$args{direction};
|
---|
347 |
|
---|
348 | my $ret = DNSDB::getRecCount($dbh, (defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
|
---|
349 | offset => $args{offset}, sortby => $args{sortby}, sortorder => $args{sortorder},
|
---|
350 | filter => $args{filter}) );
|
---|
351 |
|
---|
352 | die $DNSDB::errstr if !$ret;
|
---|
353 |
|
---|
354 | return $ret;
|
---|
355 | }
|
---|
356 |
|
---|
357 | sub addRec {
|
---|
358 | my %args = @_;
|
---|
359 |
|
---|
360 | _commoncheck(\%args, 'y');
|
---|
361 |
|
---|
362 | # note dist, weight, port are not required on all types; will be ignored if not needed.
|
---|
363 | my ($code, $msg) = DNSDB::addRec($dbh, $args{def}, $args{domid}, $args{host}, $DNSDB::typemap{$args{type}},
|
---|
364 | $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
|
---|
365 |
|
---|
366 | die $msg if $code eq 'FAIL';
|
---|
367 | }
|
---|
368 |
|
---|
369 | sub updateRec {
|
---|
370 | my %args = @_;
|
---|
371 |
|
---|
372 | _commoncheck(\%args, 'y');
|
---|
373 |
|
---|
374 | # note dist, weight, port are not required on all types; will be ignored if not needed.
|
---|
375 | my ($code, $msg) = DNSDB::updateRec($dbh, $args{def}, $args{recid}, $args{host}, $DNSDB::typemap{$args{type}},
|
---|
376 | $args{val}, $args{ttl}, $args{dist}, $args{weight}, $args{port});
|
---|
377 |
|
---|
378 | die $msg if $code eq 'FAIL';
|
---|
379 | }
|
---|
380 |
|
---|
381 | sub delRec {
|
---|
382 | my %args = @_;
|
---|
383 |
|
---|
384 | _commoncheck(\%args, 'y');
|
---|
385 |
|
---|
386 | my ($code, $msg) = DNSDB::delRec($dbh, $args{def}, $args{recid});
|
---|
387 |
|
---|
388 | die $msg if $code eq 'FAIL';
|
---|
389 | }
|
---|
390 |
|
---|
391 | #sub getLogCount {}
|
---|
392 | #sub getLogEntries {}
|
---|
393 | #sub getTypelist {}
|
---|
394 | #sub parentID {}
|
---|
395 | #sub isParent {}
|
---|
396 |
|
---|
397 | sub zoneStatus {
|
---|
398 | my %args = @_;
|
---|
399 |
|
---|
400 | _commoncheck(\%args, 'y');
|
---|
401 |
|
---|
402 | my @arglist = ($dbh, $args{zoneid});
|
---|
403 | push @arglist, $args{status} if defined($args{status});
|
---|
404 |
|
---|
405 | my $status = DNSDB::zoneStatus(@arglist);
|
---|
406 | }
|
---|
407 |
|
---|
408 | #sub importAXFR {}
|
---|
409 | #sub importBIND {}
|
---|
410 | #sub import_tinydns {}
|
---|
411 | #sub export {}
|
---|
412 | #sub __export_tiny {}
|
---|
413 | #sub _printrec_tiny {}
|
---|
414 | #sub mailNotify {}
|
---|
415 |
|
---|
416 | sub get_method_list {
|
---|
417 | my @methods = keys %{$methods};
|
---|
418 | return \@methods;
|
---|
419 | }
|
---|