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