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