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