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