1 | #!/usr/bin/perl -w -T
|
---|
2 | # XMLRPC interface to manipulate most DNS DB entities
|
---|
3 | ##
|
---|
4 | # $Id: dns-rpc.cgi 500 2013-05-07 14:44:58Z 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 | # and finally retrieve the records.
|
---|
382 | my $ret = $dnsdb->getRecList(defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
|
---|
383 | offset => $args{offset}, nrecs => $args{nrecs}, sortby => $args{sortby},
|
---|
384 | sortorder => $args{sortorder}, filter => $args{filter});
|
---|
385 |
|
---|
386 | ##fixme: what to do if domain doesn't exist?
|
---|
387 | die $dnsdb->errstr if !$ret;
|
---|
388 |
|
---|
389 | return $ret;
|
---|
390 | }
|
---|
391 |
|
---|
392 | sub getRecCount {
|
---|
393 | my %args = @_;
|
---|
394 |
|
---|
395 | _commoncheck(\%args);
|
---|
396 |
|
---|
397 | # set some optional args
|
---|
398 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
399 | $args{nstart} = 0 if !$args{nstart};
|
---|
400 | ## for order, need to map input to column names
|
---|
401 | $args{order} = 'host' if !$args{order};
|
---|
402 | $args{direction} = 'ASC' if !$args{direction};
|
---|
403 |
|
---|
404 | my $ret = $dnsdb->getRecCount($args{defrec}, $args{revrec}, $args{id}, $args{filter});
|
---|
405 |
|
---|
406 | die $dnsdb->errstr if !$ret;
|
---|
407 |
|
---|
408 | return $ret;
|
---|
409 | }
|
---|
410 |
|
---|
411 | sub addRec {
|
---|
412 | my %args = @_;
|
---|
413 |
|
---|
414 | _commoncheck(\%args, 'y');
|
---|
415 |
|
---|
416 | _loccheck(\%args);
|
---|
417 | _ttlcheck(\%args);
|
---|
418 |
|
---|
419 | # allow passing text types rather than DNS integer IDs
|
---|
420 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
421 |
|
---|
422 | my @recargs = ($args{defrec}, $args{revrec}, $args{parent_id},
|
---|
423 | \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location});
|
---|
424 | if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
425 | push @recargs, $args{distance};
|
---|
426 | if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
427 | push @recargs, $args{weight};
|
---|
428 | push @recargs, $args{port};
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | my ($code, $msg) = $dnsdb->addRec(@recargs);
|
---|
433 |
|
---|
434 | die $msg if $code eq 'FAIL';
|
---|
435 | return $msg;
|
---|
436 | }
|
---|
437 |
|
---|
438 | sub updateRec {
|
---|
439 | my %args = @_;
|
---|
440 |
|
---|
441 | _commoncheck(\%args, 'y');
|
---|
442 |
|
---|
443 | # get old line, so we can update only the bits that the caller passed to change
|
---|
444 | # note we subbed address for val since it's a little more caller-friendly
|
---|
445 | my $oldrec = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
|
---|
446 | foreach my $field (qw(name type address ttl location distance weight port)) {
|
---|
447 | $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
|
---|
448 | }
|
---|
449 |
|
---|
450 | # allow passing text types rather than DNS integer IDs
|
---|
451 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
452 |
|
---|
453 | # note dist, weight, port are not required on all types; will be ignored if not needed.
|
---|
454 | # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
|
---|
455 | my ($code, $msg) = $dnsdb->updateRec($args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
|
---|
456 | \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
|
---|
457 | $args{distance}, $args{weight}, $args{port});
|
---|
458 |
|
---|
459 | die $msg if $code eq 'FAIL';
|
---|
460 | return $msg;
|
---|
461 | }
|
---|
462 |
|
---|
463 | # Takes a passed CIDR block and DNS pattern; adds a new record or updates the record(s) affected
|
---|
464 | sub addOrUpdateRevRec {
|
---|
465 | my %args = @_;
|
---|
466 |
|
---|
467 | _commoncheck(\%args, 'y');
|
---|
468 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
469 |
|
---|
470 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
471 | if (scalar(@$zonelist) == 0) {
|
---|
472 | # enhh.... WTF?
|
---|
473 | } elsif (scalar(@$zonelist) == 1) {
|
---|
474 | # check if the single zone returned is bigger than the CIDR. if so, we can just add a record
|
---|
475 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
476 | if ($zone->contains($cidr)) {
|
---|
477 | # We need to strip the CIDR mask on IPv4 /32 assignments, or we just add a new record all the time.
|
---|
478 | my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
|
---|
479 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
480 | id => $zonelist->[0]->{rdns_id}, filter => $filt);
|
---|
481 | if (scalar(@$reclist) == 0) {
|
---|
482 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
483 | my $type = ($cidr->{isv6} ? 65284 : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
484 | addRec(defrec =>'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
485 | address => "$cidr", %args);
|
---|
486 | } else {
|
---|
487 | my $flag = 0;
|
---|
488 | foreach my $rec (@$reclist) {
|
---|
489 | # pure PTR plus composite types
|
---|
490 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281
|
---|
491 | || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
492 | next unless $rec->{val} eq $filt; # make sure we really update the record we want to update.
|
---|
493 | $dnsdb->updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
|
---|
494 | parent_id => $zonelist->[0]->{rdns_id}, %args);
|
---|
495 | $flag = 1;
|
---|
496 | last; # only do one record.
|
---|
497 | }
|
---|
498 | unless ($flag) {
|
---|
499 | # Nothing was updated, so we didn't really have a match. Add as per @$reclist==0
|
---|
500 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
501 | my $type = ($cidr->{isv6} ? 65282 : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
502 | $dnsdb->addRec(defrec =>'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
503 | address => "$cidr", %args);
|
---|
504 | }
|
---|
505 | }
|
---|
506 | } else {
|
---|
507 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
508 | } # done single-zone-contains-$cidr
|
---|
509 | } else {
|
---|
510 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
511 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
512 | foreach my $zdata (@$zonelist) {
|
---|
513 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
514 | id => $zdata->{rdns_id}, filter => $zdata->{revnet});
|
---|
515 | if (scalar(@$reclist) == 0) {
|
---|
516 | my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
517 | $dnsdb->addRec(defrec =>'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
518 | address => "$args{cidr}", %args);
|
---|
519 | } else {
|
---|
520 | foreach my $rec (@$reclist) {
|
---|
521 | # only the composite and/or template types; pure PTR or nontemplate composite
|
---|
522 | # types are nominally impossible here.
|
---|
523 | next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
524 | $dnsdb->updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
|
---|
525 | parent_id => $zdata->{rdns_id}, %args);
|
---|
526 | last; # only do one record.
|
---|
527 | }
|
---|
528 | }
|
---|
529 | } # iterate zones within $cidr
|
---|
530 | } # done $cidr-contains-zones
|
---|
531 | }
|
---|
532 |
|
---|
533 | sub delRec {
|
---|
534 | my %args = @_;
|
---|
535 |
|
---|
536 | _commoncheck(\%args, 'y');
|
---|
537 |
|
---|
538 | my ($code, $msg) = $dnsdb->delRec($args{defrec}, $args{recrev}, $args{id});
|
---|
539 |
|
---|
540 | die $msg if $code eq 'FAIL';
|
---|
541 | return $msg;
|
---|
542 | }
|
---|
543 |
|
---|
544 | sub delByCIDR {
|
---|
545 | my %args = @_;
|
---|
546 |
|
---|
547 | _commoncheck(\%args, 'y');
|
---|
548 |
|
---|
549 | # much like addOrUpdateRevRec()
|
---|
550 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
551 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
552 |
|
---|
553 | if (scalar(@$zonelist) == 0) {
|
---|
554 | # enhh.... WTF?
|
---|
555 | } elsif (scalar(@$zonelist) == 1) {
|
---|
556 |
|
---|
557 | # check if the single zone returned is bigger than the CIDR
|
---|
558 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
559 | if ($zone->contains($cidr)) {
|
---|
560 |
|
---|
561 | if ($args{delsubs}) {
|
---|
562 | # Delete ALL EVARYTHING!!one11!! in $args{cidr}
|
---|
563 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zonelist->[0]->{rdns_id});
|
---|
564 | foreach my $rec (@$reclist) {
|
---|
565 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
566 | next unless $cidr->contains($reccidr);
|
---|
567 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
568 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
569 | ##fixme: multiple records, wanna wax'em all, how to report errors?
|
---|
570 | if ($args{delforward} ||
|
---|
571 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
572 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
573 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
574 | } else {
|
---|
575 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
576 | }
|
---|
577 | }
|
---|
578 | if ($args{parpatt} && $zone == $cidr) {
|
---|
579 | # Edge case; we've just gone and axed all the records in the reverse zone.
|
---|
580 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
581 | $dnsdb->addRec(defrec =>'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id},
|
---|
582 | type => ($zone->{isv6} ? 65284 : 65283), address => "$cidr", %args);
|
---|
583 | }
|
---|
584 |
|
---|
585 | } else {
|
---|
586 | # Selectively delete only exact matches on $args{cidr}
|
---|
587 |
|
---|
588 | # We need to strip the CIDR mask on IPv4 /32 assignments, or we can't find single-IP records
|
---|
589 | my $filt = ($cidr->{isv6} || $cidr->masklen != 32 ? "$cidr" : $cidr->addr);
|
---|
590 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y',
|
---|
591 | id => $zonelist->[0]->{rdns_id}, filter => $filt, sortby => 'val', sortorder => 'DESC');
|
---|
592 | foreach my $rec (@$reclist) {
|
---|
593 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
594 | next unless $cidr == $reccidr;
|
---|
595 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
596 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
597 | if ($args{delforward} || $rec->{type} == 12) {
|
---|
598 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
599 | die $msg if $code eq 'FAIL';
|
---|
600 | return $msg;
|
---|
601 | } else {
|
---|
602 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
603 | die $dnsdb->errstr if !$ret;
|
---|
604 | return "A+PTR for $args{cidr} split and PTR removed";
|
---|
605 | }
|
---|
606 | } # foreach @$reclist
|
---|
607 | }
|
---|
608 |
|
---|
609 | } else { # $cidr > $zone but we only have one zone
|
---|
610 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
611 | return "Warning: $args{cidr} is only partly represented in DNS. Check and remove DNS records manually.";
|
---|
612 | } # done single-zone-contains-$cidr
|
---|
613 |
|
---|
614 | } else { # multiple zones nominally "contain" $cidr
|
---|
615 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
616 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
617 | foreach my $zdata (@$zonelist) {
|
---|
618 | my $reclist = $dnsdb->getRecList(defrec => 'n', revrec => 'y', id => $zdata->{rdns_id});
|
---|
619 | if (scalar(@$reclist) == 0) {
|
---|
620 | # nothing to do? or do we (re)add a record based on the parent?
|
---|
621 | # yes, yes we do, past the close of the else
|
---|
622 | # my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
623 | # addRec(defrec =>'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
624 | # address => "$args{cidr}", %args);
|
---|
625 | } else {
|
---|
626 | foreach my $rec (@$reclist) {
|
---|
627 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
628 | $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
629 | # Template types are only useful when attached to a reverse zone.
|
---|
630 | ##fixme ..... or ARE THEY?
|
---|
631 | if ($args{delforward} ||
|
---|
632 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
633 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
634 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
635 | } else {
|
---|
636 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
637 | }
|
---|
638 | } # foreach @$reclist
|
---|
639 | } # nrecs != 0
|
---|
640 | if ($args{parpatt}) {
|
---|
641 | # We've just gone and axed all the records in the reverse zone.
|
---|
642 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
643 | $dnsdb->addRec(defrec =>'n', revrec => 'y', parent_id => $zdata->{rdns_id},
|
---|
644 | type => ($cidr->{isv6} ? 65284 : 65283),
|
---|
645 | address => $zdata->{revnet}, name => $args{parpatt}, %args);
|
---|
646 | }
|
---|
647 | } # iterate zones within $cidr
|
---|
648 | } # done $cidr-contains-zones
|
---|
649 |
|
---|
650 | } # end delByCIDR()
|
---|
651 |
|
---|
652 | #sub getLogCount {}
|
---|
653 | #sub getLogEntries {}
|
---|
654 |
|
---|
655 | sub getRevPattern {
|
---|
656 | my %args = @_;
|
---|
657 |
|
---|
658 | _commoncheck(\%args, 'y');
|
---|
659 |
|
---|
660 | return $dnsdb->getRevPattern($args{cidr}, $args{group});
|
---|
661 | }
|
---|
662 |
|
---|
663 | #sub getTypelist {}
|
---|
664 | #sub parentID {}
|
---|
665 | #sub isParent {}
|
---|
666 |
|
---|
667 | sub zoneStatus {
|
---|
668 | my %args = @_;
|
---|
669 |
|
---|
670 | _commoncheck(\%args, 'y');
|
---|
671 |
|
---|
672 | my @arglist = ($args{zoneid});
|
---|
673 | push @arglist, $args{status} if defined($args{status});
|
---|
674 |
|
---|
675 | my $status = $dnsdb->zoneStatus(@arglist);
|
---|
676 | }
|
---|
677 |
|
---|
678 | # Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
|
---|
679 | sub getZonesByCIDR {
|
---|
680 | my %args = @_;
|
---|
681 |
|
---|
682 | _commoncheck(\%args, 'y');
|
---|
683 |
|
---|
684 | return $dnsdb->getZonesByCIDR(%args);
|
---|
685 | }
|
---|
686 |
|
---|
687 | #sub importAXFR {}
|
---|
688 | #sub importBIND {}
|
---|
689 | #sub import_tinydns {}
|
---|
690 | #sub export {}
|
---|
691 | #sub __export_tiny {}
|
---|
692 | #sub _printrec_tiny {}
|
---|
693 | #sub mailNotify {}
|
---|
694 |
|
---|
695 | sub get_method_list {
|
---|
696 | my @methods = keys %{$methods};
|
---|
697 | return \@methods;
|
---|
698 | }
|
---|