1 | #!/usr/bin/perl -w -T
|
---|
2 | # XMLRPC interface to manipulate most DNS DB entities
|
---|
3 | ##
|
---|
4 | # $Id: dns-rpc.cgi 673 2015-01-15 20:27:56Z kdeugau $
|
---|
5 | # Copyright 2012,2013 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 | use DNSDB;
|
---|
27 |
|
---|
28 | use FCGI;
|
---|
29 | #use Frontier::RPC2;
|
---|
30 | use Frontier::Responder;
|
---|
31 |
|
---|
32 | ## We need to handle a couple of things globally, rather than pasting the same bit into *every* sub.
|
---|
33 | ## So, let's subclass Frontier::RPC2 + Frontier::Responder, so we can override the single sub in each
|
---|
34 | ## that needs kicking
|
---|
35 | #### hmm. put this in a separate file?
|
---|
36 | #package DNSDB::RPC;
|
---|
37 | #our @ISA = ("Frontier::RPC2", "Frontier::Responder");
|
---|
38 | #package main;
|
---|
39 |
|
---|
40 | my $dnsdb = DNSDB->new();
|
---|
41 |
|
---|
42 | my $methods = {
|
---|
43 | #sub getPermissions {
|
---|
44 | #sub changePermissions {
|
---|
45 | #sub comparePermissions {
|
---|
46 | #sub changeGroup {
|
---|
47 | 'dnsdb.addDomain' => \&addDomain,
|
---|
48 | 'dnsdb.delZone' => \&delZone,
|
---|
49 | #sub domainName {
|
---|
50 | #sub revName {
|
---|
51 | 'dnsdb.domainID' => \&domainID,
|
---|
52 | #sub revID {
|
---|
53 | 'dnsdb.addRDNS' => \&addRDNS,
|
---|
54 | #sub getZoneCount {
|
---|
55 | #sub getZoneList {
|
---|
56 | #sub getZoneLocation {
|
---|
57 | 'dnsdb.addGroup' => \&addGroup,
|
---|
58 | 'dnsdb.delGroup' => \&delGroup,
|
---|
59 | #sub getChildren {
|
---|
60 | #sub groupName {
|
---|
61 | #sub getGroupCount {
|
---|
62 | #sub getGroupList {
|
---|
63 | #sub groupID {
|
---|
64 | 'dnsdb.addUser' => \&addUser,
|
---|
65 | #sub getUserCount {
|
---|
66 | #sub getUserList {
|
---|
67 | #sub getUserDropdown {
|
---|
68 | 'dnsdb.updateUser' => \&updateUser,
|
---|
69 | 'dnsdb.delUser' => \&delUser,
|
---|
70 | #sub userFullName {
|
---|
71 | #sub userStatus {
|
---|
72 | #sub getUserData {
|
---|
73 | #sub addLoc {
|
---|
74 | #sub updateLoc {
|
---|
75 | #sub delLoc {
|
---|
76 | #sub getLoc {
|
---|
77 | #sub getLocCount {
|
---|
78 | #sub getLocList {
|
---|
79 | 'dnsdb.getLocDropdown' => \&getLocDropdown,
|
---|
80 | 'dnsdb.getSOA' => \&getSOA,
|
---|
81 | #sub updateSOA {
|
---|
82 | 'dnsdb.getRecLine' => \&getRecLine,
|
---|
83 | 'dnsdb.getRecList' => \&getRecList,
|
---|
84 | 'dnsdb.getRecCount' => \&getRecCount,
|
---|
85 | 'dnsdb.addRec' => \&rpc_addRec,
|
---|
86 | 'dnsdb.updateRec' => \&updateRec,
|
---|
87 | #sub downconvert {
|
---|
88 | 'dnsdb.addOrUpdateRevRec' => \&addOrUpdateRevRec,
|
---|
89 | 'dnsdb.delRec' => \&delRec,
|
---|
90 | 'dnsdb.delByCIDR' => \&delByCIDR,
|
---|
91 | #sub getLogCount {}
|
---|
92 | #sub getLogEntries {}
|
---|
93 | 'dnsdb.getRevPattern' => \&getRevPattern,
|
---|
94 | 'dnsdb.getRevSet' => \&getRevSet,
|
---|
95 | 'dnsdb.getTypelist' => \&getTypelist,
|
---|
96 | 'dnsdb.getTypemap' => \&getTypemap,
|
---|
97 | 'dnsdb.getReverse_typemap' => \&getReverse_typemap,
|
---|
98 | #sub parentID {
|
---|
99 | #sub isParent {
|
---|
100 | 'dnsdb.zoneStatus' => \&zoneStatus,
|
---|
101 | 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
|
---|
102 | #sub importAXFR {
|
---|
103 | #sub importBIND {
|
---|
104 | #sub import_tinydns {
|
---|
105 | #sub export {
|
---|
106 | #sub mailNotify {
|
---|
107 |
|
---|
108 | 'dnsdb.getMethods' => \&get_method_list
|
---|
109 | };
|
---|
110 |
|
---|
111 | my $reqcnt = 0;
|
---|
112 |
|
---|
113 | while (FCGI::accept >= 0) {
|
---|
114 | my $res = Frontier::Responder->new(
|
---|
115 | methods => $methods
|
---|
116 | );
|
---|
117 |
|
---|
118 | # "Can't do that" errors
|
---|
119 | if (!$dnsdb) {
|
---|
120 | print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $dnsdb->err);
|
---|
121 | } else {
|
---|
122 | print $res->answer;
|
---|
123 | }
|
---|
124 | last if $reqcnt++ > $dnsdb->{maxfcgi};
|
---|
125 | } # while FCGI::accept
|
---|
126 |
|
---|
127 |
|
---|
128 | exit;
|
---|
129 |
|
---|
130 | ##
|
---|
131 | ## Subs below here
|
---|
132 | ##
|
---|
133 |
|
---|
134 | # Check RPC ACL
|
---|
135 | sub _aclcheck {
|
---|
136 | my $subsys = shift;
|
---|
137 | return 1 if grep /$ENV{REMOTE_ADDR}/, @{$dnsdb->{rpcacl}{$subsys}};
|
---|
138 | warn "$subsys/$ENV{REMOTE_ADDR} not in ACL\n"; # a bit of logging
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | # Let's see if we can factor these out of the RPC method subs
|
---|
143 | sub _commoncheck {
|
---|
144 | my $argref = shift;
|
---|
145 | my $needslog = shift;
|
---|
146 |
|
---|
147 | die "Missing remote system name\n" if !$argref->{rpcsystem};
|
---|
148 | die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
|
---|
149 | if ($needslog) {
|
---|
150 | die "Missing remote username\n" if !$argref->{rpcuser};
|
---|
151 | die "Couldn't set userdata for logging\n"
|
---|
152 | unless $dnsdb->initRPC(username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
|
---|
153 | fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) );
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | # check for defrec and revrec; only call on subs that deal with records
|
---|
158 | sub _reccheck {
|
---|
159 | my $argref = shift;
|
---|
160 | die "Missing defrec and/or revrec flags\n" if !($argref->{defrec} || $argref->{revrec});
|
---|
161 | }
|
---|
162 |
|
---|
163 | # set location to the zone's default location if none is specified
|
---|
164 | sub _loccheck {
|
---|
165 | my $argref = shift;
|
---|
166 | if (!$argref->{location} && $argref->{defrec} eq 'n') {
|
---|
167 | $argref->{location} = $dnsdb->getZoneLocation($argref->{revrec}, $argref->{parent_id});
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | # set ttl to zone defailt minttl if none is specified
|
---|
172 | sub _ttlcheck {
|
---|
173 | my $argref = shift;
|
---|
174 | if (!$argref->{ttl}) {
|
---|
175 | my $tmp = $dnsdb->getSOA($argref->{defrec}, $argref->{revrec}, $argref->{parent_id});
|
---|
176 | $argref->{ttl} = $tmp->{minttl};
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | #sub connectDB {
|
---|
181 | #sub finish {
|
---|
182 | #sub initGlobals {
|
---|
183 | #sub initPermissions {
|
---|
184 | #sub getPermissions {
|
---|
185 | #sub changePermissions {
|
---|
186 | #sub comparePermissions {
|
---|
187 | #sub changeGroup {
|
---|
188 | #sub _log {
|
---|
189 |
|
---|
190 | sub addDomain {
|
---|
191 | my %args = @_;
|
---|
192 |
|
---|
193 | _commoncheck(\%args, 'y');
|
---|
194 |
|
---|
195 | my ($code, $msg) = $dnsdb->addDomain($args{domain}, $args{group}, $args{state}, $args{location});
|
---|
196 | die "$msg\n" if $code eq 'FAIL';
|
---|
197 | return $msg; # domain ID
|
---|
198 | }
|
---|
199 |
|
---|
200 | sub delZone {
|
---|
201 | my %args = @_;
|
---|
202 |
|
---|
203 | _commoncheck(\%args, 'y');
|
---|
204 | die "Need forward/reverse zone flag\n" if !$args{revrec};
|
---|
205 |
|
---|
206 | my ($code,$msg);
|
---|
207 | # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
|
---|
208 | if ($args{zone} =~ /^\d+$/) {
|
---|
209 | ($code,$msg) = $dnsdb->delZone($args{zone}, $args{revrec});
|
---|
210 | } else {
|
---|
211 | my $zoneid;
|
---|
212 | $zoneid = $dnsdb->domainID($args{zone}) if $args{revrec} eq 'n';
|
---|
213 | $zoneid = $dnsdb->revID($args{zone}) if $args{revrec} eq 'y';
|
---|
214 | die "Can't find zone: ".$dnsdb->errstr."\n" if !$zoneid;
|
---|
215 | ($code,$msg) = $dnsdb->delZone($zoneid, $args{revrec});
|
---|
216 | }
|
---|
217 | die "$msg\n" if $code eq 'FAIL';
|
---|
218 | return $msg;
|
---|
219 | }
|
---|
220 |
|
---|
221 | #sub domainName {}
|
---|
222 | #sub revName {}
|
---|
223 |
|
---|
224 | sub domainID {
|
---|
225 | my %args = @_;
|
---|
226 |
|
---|
227 | _commoncheck(\%args, 'y');
|
---|
228 |
|
---|
229 | my $domid = $dnsdb->domainID($args{domain});
|
---|
230 | die $dnsdb->errstr."\n" if !$domid;
|
---|
231 | return $domid;
|
---|
232 | }
|
---|
233 |
|
---|
234 | #sub revID {}
|
---|
235 |
|
---|
236 | sub addRDNS {
|
---|
237 | my %args = @_;
|
---|
238 |
|
---|
239 | _commoncheck(\%args, 'y');
|
---|
240 |
|
---|
241 | my ($code, $msg) = $dnsdb->addRDNS($args{revzone}, $args{revpatt}, $args{group}, $args{state}, $args{defloc});
|
---|
242 | die "$msg\n" if $code eq 'FAIL';
|
---|
243 | return $msg; # domain ID
|
---|
244 | }
|
---|
245 |
|
---|
246 | #sub getZoneCount {}
|
---|
247 | #sub getZoneList {}
|
---|
248 | #sub getZoneLocation {}
|
---|
249 |
|
---|
250 | sub addGroup {
|
---|
251 | my %args = @_;
|
---|
252 |
|
---|
253 | _commoncheck(\%args, 'y');
|
---|
254 | die "Missing new group name\n" if !$args{groupname};
|
---|
255 | die "Missing parent group ID\n" if !$args{parent_id};
|
---|
256 |
|
---|
257 | # not sure how to usefully represent permissions via RPC. :/
|
---|
258 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
259 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
260 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
261 | };
|
---|
262 | ## optional $inhert arg?
|
---|
263 | my ($code,$msg) = $dnsdb->addGroup($args{groupname}, $args{parent_id}, $perms);
|
---|
264 | die "$msg\n" if $code eq 'FAIL';
|
---|
265 | return $msg;
|
---|
266 | }
|
---|
267 |
|
---|
268 | sub delGroup {
|
---|
269 | my %args = @_;
|
---|
270 |
|
---|
271 | _commoncheck(\%args, 'y');
|
---|
272 | die "Missing group ID or name to remove\n" if !$args{group};
|
---|
273 |
|
---|
274 | my ($code,$msg);
|
---|
275 | # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
|
---|
276 | if ($args{group} =~ /^\d+$/) {
|
---|
277 | ($code,$msg) = $dnsdb->delGroup($args{group});
|
---|
278 | } else {
|
---|
279 | my $grpid = $dnsdb->groupID($args{group});
|
---|
280 | die "Can't find group\n" if !$grpid;
|
---|
281 | ($code,$msg) = $dnsdb->delGroup($grpid);
|
---|
282 | }
|
---|
283 | die "$msg\n" if $code eq 'FAIL';
|
---|
284 | return $msg;
|
---|
285 | }
|
---|
286 |
|
---|
287 | #sub getChildren {}
|
---|
288 | #sub groupName {}
|
---|
289 | #sub getGroupCount {}
|
---|
290 | #sub getGroupList {}
|
---|
291 | #sub groupID {}
|
---|
292 |
|
---|
293 | sub addUser {
|
---|
294 | my %args = @_;
|
---|
295 |
|
---|
296 | _commoncheck(\%args, 'y');
|
---|
297 |
|
---|
298 | # not sure how to usefully represent permissions via RPC. :/
|
---|
299 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
300 | # bend and twist; get those arguments in in the right order!
|
---|
301 | $args{type} = 'u' if !$args{type};
|
---|
302 | $args{permstring} = 'i' if !defined($args{permstring});
|
---|
303 | my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
|
---|
304 | for my $argname ('fname','lname','phone') {
|
---|
305 | last if !$args{$argname};
|
---|
306 | push @userargs, $args{$argname};
|
---|
307 | }
|
---|
308 | my ($code,$msg) = $dnsdb->addUser(@userargs);
|
---|
309 | die "$msg\n" if $code eq 'FAIL';
|
---|
310 | return $msg;
|
---|
311 | }
|
---|
312 |
|
---|
313 | #sub getUserCount {}
|
---|
314 | #sub getUserList {}
|
---|
315 | #sub getUserDropdown {}
|
---|
316 | #sub checkUser {}
|
---|
317 |
|
---|
318 | sub updateUser {
|
---|
319 | my %args = @_;
|
---|
320 |
|
---|
321 | _commoncheck(\%args, 'y');
|
---|
322 |
|
---|
323 | die "Missing UID\n" if !$args{uid};
|
---|
324 |
|
---|
325 | # bend and twist; get those arguments in in the right order!
|
---|
326 | $args{type} = 'u' if !$args{type};
|
---|
327 | my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
|
---|
328 | for my $argname ('fname','lname','phone') {
|
---|
329 | last if !$args{$argname};
|
---|
330 | push @userargs, $args{$argname};
|
---|
331 | }
|
---|
332 | ##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
|
---|
333 | # have to pass them all in to be overwritten
|
---|
334 | my ($code,$msg) = $dnsdb->updateUser(@userargs);
|
---|
335 | die "$msg\n" if $code eq 'FAIL';
|
---|
336 | return $msg;
|
---|
337 | }
|
---|
338 |
|
---|
339 | sub delUser {
|
---|
340 | my %args = @_;
|
---|
341 |
|
---|
342 | _commoncheck(\%args, 'y');
|
---|
343 |
|
---|
344 | die "Missing UID\n" if !$args{uid};
|
---|
345 | my ($code,$msg) = $dnsdb->delUser($args{uid});
|
---|
346 | die "$msg\n" if $code eq 'FAIL';
|
---|
347 | return $msg;
|
---|
348 | }
|
---|
349 |
|
---|
350 | #sub userFullName {}
|
---|
351 | #sub userStatus {}
|
---|
352 | #sub getUserData {}
|
---|
353 |
|
---|
354 | #sub addLoc {}
|
---|
355 | #sub updateLoc {}
|
---|
356 | #sub delLoc {}
|
---|
357 | #sub getLoc {}
|
---|
358 | #sub getLocCount {}
|
---|
359 | #sub getLocList {}
|
---|
360 |
|
---|
361 | sub getLocDropdown {
|
---|
362 | my %args = @_;
|
---|
363 |
|
---|
364 | _commoncheck(\%args);
|
---|
365 | $args{defloc} = '' if !$args{defloc};
|
---|
366 |
|
---|
367 | my $ret = $dnsdb->getLocDropdown($args{group}, $args{defloc});
|
---|
368 | return $ret;
|
---|
369 | }
|
---|
370 |
|
---|
371 | sub getSOA {
|
---|
372 | my %args = @_;
|
---|
373 |
|
---|
374 | _commoncheck(\%args);
|
---|
375 |
|
---|
376 | _reccheck(\%args);
|
---|
377 |
|
---|
378 | my $ret = $dnsdb->getSOA($args{defrec}, $args{revrec}, $args{id});
|
---|
379 | if (!$ret) {
|
---|
380 | if ($args{defrec} eq 'y') {
|
---|
381 | die "No default SOA record in group\n";
|
---|
382 | } else {
|
---|
383 | die "No SOA record in zone\n";
|
---|
384 | }
|
---|
385 | }
|
---|
386 | return $ret;
|
---|
387 | }
|
---|
388 |
|
---|
389 | #sub updateSOA {}
|
---|
390 |
|
---|
391 | sub getRecLine {
|
---|
392 | my %args = @_;
|
---|
393 |
|
---|
394 | _commoncheck(\%args);
|
---|
395 |
|
---|
396 | _reccheck(\%args);
|
---|
397 |
|
---|
398 | my $ret = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
|
---|
399 |
|
---|
400 | die $dnsdb->errstr."\n" if !$ret;
|
---|
401 |
|
---|
402 | return $ret;
|
---|
403 | }
|
---|
404 |
|
---|
405 | sub getRecList {
|
---|
406 | my %args = @_;
|
---|
407 |
|
---|
408 | _commoncheck(\%args);
|
---|
409 |
|
---|
410 | # deal gracefully with alternate calling convention for args{id}
|
---|
411 | $args{id} = $args{ID} if !$args{id} && $args{ID};
|
---|
412 | # ... and fail if we don't have one
|
---|
413 | die "Missing zone ID\n" if !$args{id};
|
---|
414 |
|
---|
415 | # set some optional args
|
---|
416 | $args{offset} = 0 if !$args{offset};
|
---|
417 | ## for order, need to map input to column names
|
---|
418 | $args{order} = 'host' if !$args{order};
|
---|
419 | $args{direction} = 'ASC' if !$args{direction};
|
---|
420 | $args{defrec} = 'n' if !$args{defrec};
|
---|
421 | $args{revrec} = 'n' if !$args{revrec};
|
---|
422 |
|
---|
423 | # convert zone name to zone ID, if needed
|
---|
424 | if ($args{defrec} eq 'n') {
|
---|
425 | if ($args{revrec} eq 'n') {
|
---|
426 | $args{id} = $dnsdb->domainID($args{id}) if $args{id} !~ /^\d+$/;
|
---|
427 | } else {
|
---|
428 | $args{id} = $dnsdb->revID($args{id}) if $args{id} !~ /^\d+$/
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | # fail if we *still* don't have a valid zone ID
|
---|
433 | die $dnsdb->errstr."\n" if !$args{id};
|
---|
434 |
|
---|
435 | # and finally retrieve the records.
|
---|
436 | my $ret = $dnsdb->getRecList(defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
|
---|
437 | offset => $args{offset}, nrecs => $args{nrecs}, sortby => $args{sortby},
|
---|
438 | sortorder => $args{sortorder}, filter => $args{filter});
|
---|
439 | die $dnsdb->errstr."\n" if !$ret;
|
---|
440 |
|
---|
441 | return $ret;
|
---|
442 | }
|
---|
443 |
|
---|
444 | sub getRecCount {
|
---|
445 | my %args = @_;
|
---|
446 |
|
---|
447 | _commoncheck(\%args);
|
---|
448 |
|
---|
449 | _reccheck(\%args);
|
---|
450 |
|
---|
451 | # set some optional args
|
---|
452 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
453 | $args{nstart} = 0 if !$args{nstart};
|
---|
454 | ## for order, need to map input to column names
|
---|
455 | $args{order} = 'host' if !$args{order};
|
---|
456 | $args{direction} = 'ASC' if !$args{direction};
|
---|
457 |
|
---|
458 | my $ret = $dnsdb->getRecCount(defrec => $args{defrec}, revrec => $args{revrec},
|
---|
459 | id => $args{id}, filter => $args{filter});
|
---|
460 |
|
---|
461 | die $dnsdb->errstr."\n" if !$ret;
|
---|
462 |
|
---|
463 | return $ret;
|
---|
464 | }
|
---|
465 |
|
---|
466 | sub rpc_addRec {
|
---|
467 | my %args = @_;
|
---|
468 |
|
---|
469 | _commoncheck(\%args, 'y');
|
---|
470 |
|
---|
471 | _reccheck(\%args);
|
---|
472 | _loccheck(\%args);
|
---|
473 | _ttlcheck(\%args);
|
---|
474 |
|
---|
475 | # allow passing text types rather than DNS integer IDs
|
---|
476 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
477 |
|
---|
478 | my @recargs = ($args{defrec}, $args{revrec}, $args{parent_id},
|
---|
479 | \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
|
---|
480 | $args{expires}, $args{stamp});
|
---|
481 | if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
482 | push @recargs, $args{distance};
|
---|
483 | if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
484 | push @recargs, $args{weight};
|
---|
485 | push @recargs, $args{port};
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | my ($code, $msg) = $dnsdb->addRec(@recargs);
|
---|
490 |
|
---|
491 | die "$msg\n" if $code eq 'FAIL';
|
---|
492 | return $msg;
|
---|
493 | } # rpc_addRec
|
---|
494 |
|
---|
495 | sub updateRec {
|
---|
496 | my %args = @_;
|
---|
497 |
|
---|
498 | _commoncheck(\%args, 'y');
|
---|
499 |
|
---|
500 | _reccheck(\%args);
|
---|
501 |
|
---|
502 | # put some caller-friendly names in their rightful DB column places
|
---|
503 | $args{val} = $args{address};
|
---|
504 | $args{host} = $args{name};
|
---|
505 |
|
---|
506 | # get old line, so we can update only the bits that the caller passed to change
|
---|
507 | my $oldrec = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
|
---|
508 | foreach my $field (qw(host type val ttl location expires distance weight port)) {
|
---|
509 | $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
|
---|
510 | }
|
---|
511 | # stamp has special handling when blank or 0. "undefined" from the caller should mean "don't change"
|
---|
512 | $args{stamp} = $oldrec->{stamp} if !defined($args{stamp}) && $oldrec->{stampactive};
|
---|
513 |
|
---|
514 | # allow passing text types rather than DNS integer IDs
|
---|
515 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
516 |
|
---|
517 | # note dist, weight, port are not required on all types; will be ignored if not needed.
|
---|
518 | # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
|
---|
519 | my ($code, $msg) = $dnsdb->updateRec($args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
|
---|
520 | \$args{host}, \$args{type}, \$args{val}, $args{ttl}, $args{location},
|
---|
521 | $args{expires}, $args{stamp},
|
---|
522 | $args{distance}, $args{weight}, $args{port});
|
---|
523 |
|
---|
524 | die "$msg\n" if $code eq 'FAIL';
|
---|
525 | return $msg;
|
---|
526 | }
|
---|
527 |
|
---|
528 | # Takes a passed CIDR block and DNS pattern; adds a new record or updates the record(s) affected
|
---|
529 | sub addOrUpdateRevRec {
|
---|
530 | my %args = @_;
|
---|
531 |
|
---|
532 | _commoncheck(\%args, 'y');
|
---|
533 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
534 |
|
---|
535 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
536 | if (scalar(@$zonelist) == 0) {
|
---|
537 | # enhh.... WTF?
|
---|
538 | } elsif (scalar(@$zonelist) == 1) {
|
---|
539 | # check if the single zone returned is bigger than the CIDR. if so, we can just add a record
|
---|
540 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
541 | if ($zone->contains($cidr)) {
|
---|
542 | # We need to strip the CIDR mask on IPv4 /32 assignments, or we just add a new record all the time.
|
---|
543 | my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
|
---|
544 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
545 | id => $zonelist->[0]->{rdns_id}, filter => $filt);
|
---|
546 | ##fixme: Figure some new magic to automerge new incoming A(AAA)+PTR requests
|
---|
547 | # with existing A records to prevent duplication of A(AAA) records
|
---|
548 | if (scalar(@$reclist) == 0) {
|
---|
549 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
550 | my $type = ($cidr->{isv6} ? ($cidr->masklen == 128 ? 65281 : 65284) : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
551 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
552 | address => "$cidr", %args);
|
---|
553 | } else {
|
---|
554 | my $flag = 0;
|
---|
555 | foreach my $rec (@$reclist) {
|
---|
556 | # pure PTR plus composite types
|
---|
557 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281
|
---|
558 | || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
559 | next unless $rec->{val} eq $filt; # make sure we really update the record we want to update.
|
---|
560 | updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
|
---|
561 | parent_id => $zonelist->[0]->{rdns_id}, address => "$cidr", %args);
|
---|
562 | $flag = 1;
|
---|
563 | last; # only do one record.
|
---|
564 | }
|
---|
565 | unless ($flag) {
|
---|
566 | # Nothing was updated, so we didn't really have a match. Add as per @$reclist==0
|
---|
567 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
568 | my $type = ($cidr->{isv6} ? 65282 : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
569 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
570 | address => "$cidr", %args);
|
---|
571 | }
|
---|
572 | }
|
---|
573 | } else {
|
---|
574 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
575 | } # done single-zone-contains-$cidr
|
---|
576 | } else {
|
---|
577 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
578 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
579 | foreach my $zdata (@$zonelist) {
|
---|
580 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
581 | id => $zdata->{rdns_id}, filter => $zdata->{revnet});
|
---|
582 | if (scalar(@$reclist) == 0) {
|
---|
583 | my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
584 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
585 | address => "$args{cidr}", %args);
|
---|
586 | } else {
|
---|
587 | foreach my $rec (@$reclist) {
|
---|
588 | # only the composite and/or template types; pure PTR or nontemplate composite
|
---|
589 | # types are nominally impossible here.
|
---|
590 | next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
591 | $dnsdb->updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
|
---|
592 | parent_id => $zdata->{rdns_id}, %args);
|
---|
593 | last; # only do one record.
|
---|
594 | }
|
---|
595 | }
|
---|
596 | } # iterate zones within $cidr
|
---|
597 | } # done $cidr-contains-zones
|
---|
598 | }
|
---|
599 |
|
---|
600 | sub delRec {
|
---|
601 | my %args = @_;
|
---|
602 |
|
---|
603 | _commoncheck(\%args, 'y');
|
---|
604 |
|
---|
605 | _reccheck(\%args);
|
---|
606 |
|
---|
607 | my ($code, $msg) = $dnsdb->delRec($args{defrec}, $args{recrev}, $args{id});
|
---|
608 |
|
---|
609 | die "$msg\n" if $code eq 'FAIL';
|
---|
610 | return $msg;
|
---|
611 | }
|
---|
612 |
|
---|
613 | sub delByCIDR {
|
---|
614 | my %args = @_;
|
---|
615 |
|
---|
616 | _commoncheck(\%args, 'y');
|
---|
617 |
|
---|
618 | # much like addOrUpdateRevRec()
|
---|
619 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
620 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
621 |
|
---|
622 | if (scalar(@$zonelist) == 0) {
|
---|
623 | # enhh.... WTF?
|
---|
624 | } elsif (scalar(@$zonelist) == 1) {
|
---|
625 |
|
---|
626 | # check if the single zone returned is bigger than the CIDR
|
---|
627 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
628 | if ($zone->contains($cidr)) {
|
---|
629 |
|
---|
630 | if ($args{delsubs}) {
|
---|
631 | # Delete ALL EVARYTHING!!one11!! in $args{cidr}
|
---|
632 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zonelist->[0]->{rdns_id});
|
---|
633 | foreach my $rec (@$reclist) {
|
---|
634 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
635 | next unless $cidr->contains($reccidr);
|
---|
636 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
637 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
638 | ##fixme: multiple records, wanna wax'em all, how to report errors?
|
---|
639 | if ($args{delforward} ||
|
---|
640 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
641 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
642 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
643 | } else {
|
---|
644 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
645 | }
|
---|
646 | }
|
---|
647 | if ($args{parpatt} && $zone == $cidr) {
|
---|
648 | # Edge case; we've just gone and axed all the records in the reverse zone.
|
---|
649 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
650 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id},
|
---|
651 | type => ($zone->{isv6} ? 65284 : 65283), address => "$cidr", name => $args{parpatt}, %args);
|
---|
652 | }
|
---|
653 |
|
---|
654 | } else {
|
---|
655 | # Selectively delete only exact matches on $args{cidr}
|
---|
656 |
|
---|
657 | # We need to strip the CIDR mask on IPv4 /32 assignments, or we can't find single-IP records
|
---|
658 | my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
|
---|
659 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
660 | id => $zonelist->[0]->{rdns_id}, filter => $filt, sortby => 'val', sortorder => 'DESC');
|
---|
661 | foreach my $rec (@$reclist) {
|
---|
662 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
663 | next unless $cidr == $reccidr;
|
---|
664 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
665 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
666 | if ($args{delforward} || $rec->{type} == 12) {
|
---|
667 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
668 | die "$msg\n" if $code eq 'FAIL';
|
---|
669 | return $msg;
|
---|
670 | } else {
|
---|
671 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
672 | die $dnsdb->errstr."\n" if !$ret;
|
---|
673 | return "A+PTR for $args{cidr} split and PTR removed";
|
---|
674 | }
|
---|
675 | } # foreach @$reclist
|
---|
676 | }
|
---|
677 |
|
---|
678 | } else { # $cidr > $zone but we only have one zone
|
---|
679 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
680 | return "Warning: $args{cidr} is only partly represented in DNS. Check and remove DNS records manually.";
|
---|
681 | } # done single-zone-contains-$cidr
|
---|
682 |
|
---|
683 | } else { # multiple zones nominally "contain" $cidr
|
---|
684 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
685 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
686 | foreach my $zdata (@$zonelist) {
|
---|
687 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zdata->{rdns_id});
|
---|
688 | if (scalar(@$reclist) == 0) {
|
---|
689 | # nothing to do? or do we (re)add a record based on the parent?
|
---|
690 | # yes, yes we do, past the close of the else
|
---|
691 | # my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
692 | # rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
693 | # address => "$args{cidr}", %args);
|
---|
694 | } else {
|
---|
695 | foreach my $rec (@$reclist) {
|
---|
696 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
697 | $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
698 | # Template types are only useful when attached to a reverse zone.
|
---|
699 | ##fixme ..... or ARE THEY?
|
---|
700 | if ($args{delforward} ||
|
---|
701 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
702 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
703 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
704 | } else {
|
---|
705 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
706 | }
|
---|
707 | } # foreach @$reclist
|
---|
708 | } # nrecs != 0
|
---|
709 | if ($args{parpatt}) {
|
---|
710 | # We've just gone and axed all the records in the reverse zone.
|
---|
711 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
712 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id},
|
---|
713 | type => ($cidr->{isv6} ? 65284 : 65283),
|
---|
714 | address => $zdata->{revnet}, name => $args{parpatt}, %args);
|
---|
715 | }
|
---|
716 | } # iterate zones within $cidr
|
---|
717 | } # done $cidr-contains-zones
|
---|
718 |
|
---|
719 | } # end delByCIDR()
|
---|
720 |
|
---|
721 | #sub getLogCount {}
|
---|
722 | #sub getLogEntries {}
|
---|
723 |
|
---|
724 | sub getRevPattern {
|
---|
725 | my %args = @_;
|
---|
726 |
|
---|
727 | _commoncheck(\%args, 'y');
|
---|
728 |
|
---|
729 | return $dnsdb->getRevPattern($args{cidr}, $args{group});
|
---|
730 | }
|
---|
731 |
|
---|
732 | sub getRevSet {
|
---|
733 | my %args = @_;
|
---|
734 |
|
---|
735 | _commoncheck(\%args, 'y');
|
---|
736 |
|
---|
737 | return $dnsdb->getRevSet($args{cidr}, $args{group});
|
---|
738 | }
|
---|
739 |
|
---|
740 | sub getTypelist {
|
---|
741 | my %args = @_;
|
---|
742 | _commoncheck(\%args, 'y');
|
---|
743 |
|
---|
744 | $args{selected} = $reverse_typemap{A} if !$args{selected};
|
---|
745 |
|
---|
746 | return $dnsdb->getTypelist($args{recgroup}, $args{selected});
|
---|
747 | }
|
---|
748 |
|
---|
749 | sub getTypemap {
|
---|
750 | my %args = @_;
|
---|
751 | _commoncheck(\%args, 'y');
|
---|
752 | return \%typemap;
|
---|
753 | }
|
---|
754 |
|
---|
755 | sub getReverse_typemap {
|
---|
756 | my %args = @_;
|
---|
757 | _commoncheck(\%args, 'y');
|
---|
758 | return \%reverse_typemap;
|
---|
759 | }
|
---|
760 |
|
---|
761 | #sub parentID {}
|
---|
762 | #sub isParent {}
|
---|
763 |
|
---|
764 | sub zoneStatus {
|
---|
765 | my %args = @_;
|
---|
766 |
|
---|
767 | _commoncheck(\%args, 'y');
|
---|
768 |
|
---|
769 | my @arglist = ($args{zoneid});
|
---|
770 | push @arglist, $args{status} if defined($args{status});
|
---|
771 |
|
---|
772 | my $status = $dnsdb->zoneStatus(@arglist);
|
---|
773 | }
|
---|
774 |
|
---|
775 | # Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
|
---|
776 | sub getZonesByCIDR {
|
---|
777 | my %args = @_;
|
---|
778 |
|
---|
779 | _commoncheck(\%args, 'y');
|
---|
780 |
|
---|
781 | return $dnsdb->getZonesByCIDR(%args);
|
---|
782 | }
|
---|
783 |
|
---|
784 | #sub importAXFR {}
|
---|
785 | #sub importBIND {}
|
---|
786 | #sub import_tinydns {}
|
---|
787 | #sub export {}
|
---|
788 | #sub __export_tiny {}
|
---|
789 | #sub _printrec_tiny {}
|
---|
790 | #sub mailNotify {}
|
---|
791 |
|
---|
792 | sub get_method_list {
|
---|
793 | my @methods = keys %{$methods};
|
---|
794 | return \@methods;
|
---|
795 | }
|
---|