[216] | 1 | #!/usr/bin/perl -w -T
|
---|
[262] | 2 | # XMLRPC interface to manipulate most DNS DB entities
|
---|
| 3 | ##
|
---|
[200] | 4 | # $Id: dns-rpc.cgi 749 2017-02-15 21:37:51Z kdeugau $
|
---|
[745] | 5 | # Copyright 2012-2016 Kris Deugau <kdeugau@deepnet.cx>
|
---|
[262] | 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 | ##
|
---|
[119] | 20 |
|
---|
| 21 | use strict;
|
---|
| 22 | use warnings;
|
---|
[216] | 23 |
|
---|
| 24 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 25 | use lib '.'; ##uselib##
|
---|
[490] | 26 | use DNSDB;
|
---|
[216] | 27 |
|
---|
[490] | 28 | use FCGI;
|
---|
[119] | 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 |
|
---|
[468] | 40 | my $dnsdb = DNSDB->new();
|
---|
[191] | 41 |
|
---|
[119] | 42 | my $methods = {
|
---|
[515] | 43 | #sub getPermissions {
|
---|
| 44 | #sub changePermissions {
|
---|
| 45 | #sub comparePermissions {
|
---|
| 46 | #sub changeGroup {
|
---|
[119] | 47 | 'dnsdb.addDomain' => \&addDomain,
|
---|
[401] | 48 | 'dnsdb.delZone' => \&delZone,
|
---|
[515] | 49 | #sub domainName {
|
---|
| 50 | #sub revName {
|
---|
[500] | 51 | 'dnsdb.domainID' => \&domainID,
|
---|
[515] | 52 | #sub revID {
|
---|
[405] | 53 | 'dnsdb.addRDNS' => \&addRDNS,
|
---|
[515] | 54 | #sub getZoneCount {
|
---|
| 55 | #sub getZoneList {
|
---|
| 56 | #sub getZoneLocation {
|
---|
[121] | 57 | 'dnsdb.addGroup' => \&addGroup,
|
---|
| 58 | 'dnsdb.delGroup' => \&delGroup,
|
---|
[515] | 59 | #sub getChildren {
|
---|
| 60 | #sub groupName {
|
---|
| 61 | #sub getGroupCount {
|
---|
| 62 | #sub getGroupList {
|
---|
| 63 | #sub groupID {
|
---|
[121] | 64 | 'dnsdb.addUser' => \&addUser,
|
---|
[515] | 65 | #sub getUserCount {
|
---|
| 66 | #sub getUserList {
|
---|
| 67 | #sub getUserDropdown {
|
---|
[121] | 68 | 'dnsdb.updateUser' => \&updateUser,
|
---|
| 69 | 'dnsdb.delUser' => \&delUser,
|
---|
[515] | 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 {
|
---|
[447] | 79 | 'dnsdb.getLocDropdown' => \&getLocDropdown,
|
---|
[121] | 80 | 'dnsdb.getSOA' => \&getSOA,
|
---|
[515] | 81 | #sub updateSOA {
|
---|
[123] | 82 | 'dnsdb.getRecLine' => \&getRecLine,
|
---|
[495] | 83 | 'dnsdb.getRecList' => \&getRecList,
|
---|
[123] | 84 | 'dnsdb.getRecCount' => \&getRecCount,
|
---|
[659] | 85 | 'dnsdb.addRec' => \&rpc_addRec,
|
---|
[675] | 86 | 'dnsdb.updateRec' => \&rpc_updateRec,
|
---|
[515] | 87 | #sub downconvert {
|
---|
[453] | 88 | 'dnsdb.addOrUpdateRevRec' => \&addOrUpdateRevRec,
|
---|
[676] | 89 | 'dnsdb.updateRevSet' => \&updateRevSet,
|
---|
[680] | 90 | 'dnsdb.splitTemplate' => \&splitTemplate,
|
---|
[681] | 91 | 'dnsdb.resizeTemplate' => \&resizeTemplate,
|
---|
[682] | 92 | 'dnsdb.templatesToRecords' => \&templatesToRecords,
|
---|
[123] | 93 | 'dnsdb.delRec' => \&delRec,
|
---|
[459] | 94 | 'dnsdb.delByCIDR' => \&delByCIDR,
|
---|
[684] | 95 | 'dnsdb.delRevSet' => \&delRevSet,
|
---|
[452] | 96 | #sub getLogCount {}
|
---|
| 97 | #sub getLogEntries {}
|
---|
| 98 | 'dnsdb.getRevPattern' => \&getRevPattern,
|
---|
[673] | 99 | 'dnsdb.getRevSet' => \&getRevSet,
|
---|
[506] | 100 | 'dnsdb.getTypelist' => \&getTypelist,
|
---|
[504] | 101 | 'dnsdb.getTypemap' => \&getTypemap,
|
---|
| 102 | 'dnsdb.getReverse_typemap' => \&getReverse_typemap,
|
---|
[515] | 103 | #sub parentID {
|
---|
| 104 | #sub isParent {
|
---|
[405] | 105 | 'dnsdb.zoneStatus' => \&zoneStatus,
|
---|
[452] | 106 | 'dnsdb.getZonesByCIDR' => \&getZonesByCIDR,
|
---|
[515] | 107 | #sub importAXFR {
|
---|
| 108 | #sub importBIND {
|
---|
| 109 | #sub import_tinydns {
|
---|
| 110 | #sub export {
|
---|
| 111 | #sub mailNotify {
|
---|
[121] | 112 |
|
---|
[119] | 113 | 'dnsdb.getMethods' => \&get_method_list
|
---|
| 114 | };
|
---|
| 115 |
|
---|
[490] | 116 | my $reqcnt = 0;
|
---|
[748] | 117 | my $req = FCGI::Request();
|
---|
[490] | 118 |
|
---|
[748] | 119 | while ($req->Accept() >= 0) {
|
---|
[490] | 120 | my $res = Frontier::Responder->new(
|
---|
[119] | 121 | methods => $methods
|
---|
| 122 | );
|
---|
| 123 |
|
---|
[490] | 124 | # "Can't do that" errors
|
---|
| 125 | if (!$dnsdb) {
|
---|
| 126 | print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $dnsdb->err);
|
---|
| 127 | } else {
|
---|
| 128 | print $res->answer;
|
---|
| 129 | }
|
---|
| 130 | last if $reqcnt++ > $dnsdb->{maxfcgi};
|
---|
| 131 | } # while FCGI::accept
|
---|
[119] | 132 |
|
---|
| 133 |
|
---|
| 134 | exit;
|
---|
| 135 |
|
---|
| 136 | ##
|
---|
| 137 | ## Subs below here
|
---|
| 138 | ##
|
---|
| 139 |
|
---|
[710] | 140 | ##
|
---|
| 141 | ## Internal utility subs
|
---|
| 142 | ##
|
---|
| 143 |
|
---|
[490] | 144 | # Check RPC ACL
|
---|
[401] | 145 | sub _aclcheck {
|
---|
| 146 | my $subsys = shift;
|
---|
[486] | 147 | return 1 if grep /$ENV{REMOTE_ADDR}/, @{$dnsdb->{rpcacl}{$subsys}};
|
---|
[505] | 148 | warn "$subsys/$ENV{REMOTE_ADDR} not in ACL\n"; # a bit of logging
|
---|
[401] | 149 | return 0;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[405] | 152 | # Let's see if we can factor these out of the RPC method subs
|
---|
| 153 | sub _commoncheck {
|
---|
| 154 | my $argref = shift;
|
---|
| 155 | my $needslog = shift;
|
---|
| 156 |
|
---|
| 157 | die "Missing remote system name\n" if !$argref->{rpcsystem};
|
---|
| 158 | die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
|
---|
| 159 | if ($needslog) {
|
---|
| 160 | die "Missing remote username\n" if !$argref->{rpcuser};
|
---|
| 161 | die "Couldn't set userdata for logging\n"
|
---|
[486] | 162 | unless $dnsdb->initRPC(username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
|
---|
| 163 | fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) );
|
---|
[405] | 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[511] | 167 | # check for defrec and revrec; only call on subs that deal with records
|
---|
| 168 | sub _reccheck {
|
---|
| 169 | my $argref = shift;
|
---|
| 170 | die "Missing defrec and/or revrec flags\n" if !($argref->{defrec} || $argref->{revrec});
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[453] | 173 | # set location to the zone's default location if none is specified
|
---|
| 174 | sub _loccheck {
|
---|
| 175 | my $argref = shift;
|
---|
| 176 | if (!$argref->{location} && $argref->{defrec} eq 'n') {
|
---|
[477] | 177 | $argref->{location} = $dnsdb->getZoneLocation($argref->{revrec}, $argref->{parent_id});
|
---|
[453] | 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[710] | 181 | # set ttl to zone default minttl if none is specified
|
---|
[453] | 182 | sub _ttlcheck {
|
---|
| 183 | my $argref = shift;
|
---|
| 184 | if (!$argref->{ttl}) {
|
---|
[486] | 185 | my $tmp = $dnsdb->getSOA($argref->{defrec}, $argref->{revrec}, $argref->{parent_id});
|
---|
[453] | 186 | $argref->{ttl} = $tmp->{minttl};
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[710] | 190 | # Check if the hashrefs passed in refer to identical record data, so we can skip
|
---|
| 191 | # the actual update if nothing has actually changed. This is mainly useful for
|
---|
| 192 | # reducing log noise due to chained calls orginating with updateRevSet() since
|
---|
| 193 | # "many" records could be sent for update but only one or two have actually changed.
|
---|
| 194 | sub _checkRecMod {
|
---|
| 195 | my $oldrec = shift;
|
---|
| 196 | my $newrec = shift;
|
---|
| 197 |
|
---|
| 198 | # Because we don't know which fields we've even been passed
|
---|
| 199 | no warnings qw(uninitialized);
|
---|
| 200 |
|
---|
| 201 | my $modflag = 0;
|
---|
| 202 | # order by most common change. host should be first, due to rDNS RPC calls
|
---|
[749] | 203 | for my $field (qw(host type val)) {
|
---|
[710] | 204 | return 1 if (
|
---|
| 205 | defined($newrec->{$field}) &&
|
---|
| 206 | $oldrec->{$field} ne $newrec->{$field} );
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | return 0;
|
---|
| 210 | } # _checRecMod
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | ##
|
---|
| 214 | ## Shims for DNSDB core subs
|
---|
| 215 | ##
|
---|
| 216 |
|
---|
[119] | 217 | #sub connectDB {
|
---|
| 218 | #sub finish {
|
---|
| 219 | #sub initGlobals {
|
---|
| 220 | #sub initPermissions {
|
---|
| 221 | #sub getPermissions {
|
---|
| 222 | #sub changePermissions {
|
---|
| 223 | #sub comparePermissions {
|
---|
| 224 | #sub changeGroup {
|
---|
| 225 | #sub _log {
|
---|
| 226 |
|
---|
| 227 | sub addDomain {
|
---|
| 228 | my %args = @_;
|
---|
| 229 |
|
---|
[405] | 230 | _commoncheck(\%args, 'y');
|
---|
[119] | 231 |
|
---|
[687] | 232 | my ($code, $msg) = $dnsdb->addDomain($args{domain}, $args{group}, $args{state}, $args{defloc});
|
---|
[511] | 233 | die "$msg\n" if $code eq 'FAIL';
|
---|
[119] | 234 | return $msg; # domain ID
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[401] | 237 | sub delZone {
|
---|
[119] | 238 | my %args = @_;
|
---|
| 239 |
|
---|
[405] | 240 | _commoncheck(\%args, 'y');
|
---|
| 241 | die "Need forward/reverse zone flag\n" if !$args{revrec};
|
---|
[687] | 242 | die "Need zone identifier\n" if !$args{zone};
|
---|
[119] | 243 |
|
---|
[121] | 244 | my ($code,$msg);
|
---|
[405] | 245 | # Let's be nice; delete based on zone id OR zone name. Saves an RPC call round-trip, maybe.
|
---|
| 246 | if ($args{zone} =~ /^\d+$/) {
|
---|
[477] | 247 | ($code,$msg) = $dnsdb->delZone($args{zone}, $args{revrec});
|
---|
[119] | 248 | } else {
|
---|
[721] | 249 | die "Need zone location\n" if !defined($args{location});
|
---|
[405] | 250 | my $zoneid;
|
---|
[721] | 251 | $zoneid = $dnsdb->domainID($args{zone}, $args{location}) if $args{revrec} eq 'n';
|
---|
| 252 | $zoneid = $dnsdb->revID($args{zone}, $args{location}) if $args{revrec} eq 'y';
|
---|
[671] | 253 | die "Can't find zone: ".$dnsdb->errstr."\n" if !$zoneid;
|
---|
[477] | 254 | ($code,$msg) = $dnsdb->delZone($zoneid, $args{revrec});
|
---|
[119] | 255 | }
|
---|
[511] | 256 | die "$msg\n" if $code eq 'FAIL';
|
---|
[405] | 257 | return $msg;
|
---|
[687] | 258 | } # delZone()
|
---|
[119] | 259 |
|
---|
[405] | 260 | #sub domainName {}
|
---|
| 261 | #sub revName {}
|
---|
[500] | 262 |
|
---|
| 263 | sub domainID {
|
---|
| 264 | my %args = @_;
|
---|
| 265 |
|
---|
| 266 | _commoncheck(\%args, 'y');
|
---|
| 267 |
|
---|
[721] | 268 | my $domid = $dnsdb->domainID($args{domain}, $args{location});
|
---|
[671] | 269 | die $dnsdb->errstr."\n" if !$domid;
|
---|
[500] | 270 | return $domid;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[405] | 273 | #sub revID {}
|
---|
[119] | 274 |
|
---|
[405] | 275 | sub addRDNS {
|
---|
| 276 | my %args = @_;
|
---|
| 277 |
|
---|
| 278 | _commoncheck(\%args, 'y');
|
---|
| 279 |
|
---|
[477] | 280 | my ($code, $msg) = $dnsdb->addRDNS($args{revzone}, $args{revpatt}, $args{group}, $args{state}, $args{defloc});
|
---|
[447] | 281 | die "$msg\n" if $code eq 'FAIL';
|
---|
[687] | 282 | return $msg; # zone ID
|
---|
[405] | 283 | }
|
---|
| 284 |
|
---|
| 285 | #sub getZoneCount {}
|
---|
| 286 | #sub getZoneList {}
|
---|
| 287 | #sub getZoneLocation {}
|
---|
| 288 |
|
---|
[119] | 289 | sub addGroup {
|
---|
| 290 | my %args = @_;
|
---|
| 291 |
|
---|
[405] | 292 | _commoncheck(\%args, 'y');
|
---|
[407] | 293 | die "Missing new group name\n" if !$args{groupname};
|
---|
| 294 | die "Missing parent group ID\n" if !$args{parent_id};
|
---|
[119] | 295 |
|
---|
[407] | 296 | # not sure how to usefully represent permissions via RPC. :/
|
---|
[121] | 297 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
[119] | 298 | my $perms = {domain_edit => 1, domain_create => 1, domain_delete => 1,
|
---|
| 299 | record_edit => 1, record_create => 1, record_delete => 1
|
---|
| 300 | };
|
---|
| 301 | ## optional $inhert arg?
|
---|
[476] | 302 | my ($code,$msg) = $dnsdb->addGroup($args{groupname}, $args{parent_id}, $perms);
|
---|
[511] | 303 | die "$msg\n" if $code eq 'FAIL';
|
---|
[119] | 304 | return $msg;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | sub delGroup {
|
---|
| 308 | my %args = @_;
|
---|
| 309 |
|
---|
[405] | 310 | _commoncheck(\%args, 'y');
|
---|
[407] | 311 | die "Missing group ID or name to remove\n" if !$args{group};
|
---|
[119] | 312 |
|
---|
[121] | 313 | my ($code,$msg);
|
---|
[119] | 314 | # Let's be nice; delete based on groupid OR group name. Saves an RPC call round-trip, maybe.
|
---|
| 315 | if ($args{group} =~ /^\d+$/) {
|
---|
[476] | 316 | ($code,$msg) = $dnsdb->delGroup($args{group});
|
---|
[119] | 317 | } else {
|
---|
[486] | 318 | my $grpid = $dnsdb->groupID($args{group});
|
---|
[407] | 319 | die "Can't find group\n" if !$grpid;
|
---|
[476] | 320 | ($code,$msg) = $dnsdb->delGroup($grpid);
|
---|
[119] | 321 | }
|
---|
[511] | 322 | die "$msg\n" if $code eq 'FAIL';
|
---|
[407] | 323 | return $msg;
|
---|
[119] | 324 | }
|
---|
| 325 |
|
---|
[405] | 326 | #sub getChildren {}
|
---|
| 327 | #sub groupName {}
|
---|
| 328 | #sub getGroupCount {}
|
---|
| 329 | #sub getGroupList {}
|
---|
| 330 | #sub groupID {}
|
---|
[119] | 331 |
|
---|
| 332 | sub addUser {
|
---|
| 333 | my %args = @_;
|
---|
| 334 |
|
---|
[405] | 335 | _commoncheck(\%args, 'y');
|
---|
[119] | 336 |
|
---|
[409] | 337 | # not sure how to usefully represent permissions via RPC. :/
|
---|
[121] | 338 | # not to mention, permissions are checked at the UI layer, not the DB layer.
|
---|
[119] | 339 | # bend and twist; get those arguments in in the right order!
|
---|
| 340 | $args{type} = 'u' if !$args{type};
|
---|
| 341 | $args{permstring} = 'i' if !defined($args{permstring});
|
---|
| 342 | my @userargs = ($args{username}, $args{group}, $args{pass}, $args{state}, $args{type}, $args{permstring});
|
---|
| 343 | for my $argname ('fname','lname','phone') {
|
---|
| 344 | last if !$args{$argname};
|
---|
| 345 | push @userargs, $args{$argname};
|
---|
| 346 | }
|
---|
[479] | 347 | my ($code,$msg) = $dnsdb->addUser(@userargs);
|
---|
[511] | 348 | die "$msg\n" if $code eq 'FAIL';
|
---|
[119] | 349 | return $msg;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[405] | 352 | #sub getUserCount {}
|
---|
| 353 | #sub getUserList {}
|
---|
| 354 | #sub getUserDropdown {}
|
---|
| 355 | #sub checkUser {}
|
---|
[119] | 356 |
|
---|
| 357 | sub updateUser {
|
---|
| 358 | my %args = @_;
|
---|
| 359 |
|
---|
[405] | 360 | _commoncheck(\%args, 'y');
|
---|
[119] | 361 |
|
---|
[401] | 362 | die "Missing UID\n" if !$args{uid};
|
---|
[121] | 363 |
|
---|
[119] | 364 | # bend and twist; get those arguments in in the right order!
|
---|
[411] | 365 | $args{type} = 'u' if !$args{type};
|
---|
[119] | 366 | my @userargs = ($args{uid}, $args{username}, $args{group}, $args{pass}, $args{state}, $args{type});
|
---|
| 367 | for my $argname ('fname','lname','phone') {
|
---|
| 368 | last if !$args{$argname};
|
---|
| 369 | push @userargs, $args{$argname};
|
---|
| 370 | }
|
---|
| 371 | ##fixme: also underlying in DNSDB::updateUser(): no way to just update this or that attribute;
|
---|
| 372 | # have to pass them all in to be overwritten
|
---|
[479] | 373 | my ($code,$msg) = $dnsdb->updateUser(@userargs);
|
---|
[511] | 374 | die "$msg\n" if $code eq 'FAIL';
|
---|
[412] | 375 | return $msg;
|
---|
[119] | 376 | }
|
---|
| 377 |
|
---|
| 378 | sub delUser {
|
---|
| 379 | my %args = @_;
|
---|
| 380 |
|
---|
[405] | 381 | _commoncheck(\%args, 'y');
|
---|
[119] | 382 |
|
---|
[401] | 383 | die "Missing UID\n" if !$args{uid};
|
---|
[479] | 384 | my ($code,$msg) = $dnsdb->delUser($args{uid});
|
---|
[511] | 385 | die "$msg\n" if $code eq 'FAIL';
|
---|
[412] | 386 | return $msg;
|
---|
[119] | 387 | }
|
---|
| 388 |
|
---|
[405] | 389 | #sub userFullName {}
|
---|
| 390 | #sub userStatus {}
|
---|
| 391 | #sub getUserData {}
|
---|
[119] | 392 |
|
---|
[405] | 393 | #sub addLoc {}
|
---|
| 394 | #sub updateLoc {}
|
---|
| 395 | #sub delLoc {}
|
---|
| 396 | #sub getLoc {}
|
---|
| 397 | #sub getLocCount {}
|
---|
| 398 | #sub getLocList {}
|
---|
| 399 |
|
---|
[447] | 400 | sub getLocDropdown {
|
---|
| 401 | my %args = @_;
|
---|
| 402 |
|
---|
| 403 | _commoncheck(\%args);
|
---|
| 404 | $args{defloc} = '' if !$args{defloc};
|
---|
| 405 |
|
---|
[480] | 406 | my $ret = $dnsdb->getLocDropdown($args{group}, $args{defloc});
|
---|
[447] | 407 | return $ret;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
[119] | 410 | sub getSOA {
|
---|
| 411 | my %args = @_;
|
---|
| 412 |
|
---|
[405] | 413 | _commoncheck(\%args);
|
---|
[121] | 414 |
|
---|
[511] | 415 | _reccheck(\%args);
|
---|
| 416 |
|
---|
[481] | 417 | my $ret = $dnsdb->getSOA($args{defrec}, $args{revrec}, $args{id});
|
---|
[413] | 418 | if (!$ret) {
|
---|
| 419 | if ($args{defrec} eq 'y') {
|
---|
[401] | 420 | die "No default SOA record in group\n";
|
---|
[121] | 421 | } else {
|
---|
[413] | 422 | die "No SOA record in zone\n";
|
---|
[121] | 423 | }
|
---|
| 424 | }
|
---|
[413] | 425 | return $ret;
|
---|
[119] | 426 | }
|
---|
| 427 |
|
---|
[405] | 428 | #sub updateSOA {}
|
---|
| 429 |
|
---|
[119] | 430 | sub getRecLine {
|
---|
| 431 | my %args = @_;
|
---|
| 432 |
|
---|
[405] | 433 | _commoncheck(\%args);
|
---|
[123] | 434 |
|
---|
[511] | 435 | _reccheck(\%args);
|
---|
| 436 |
|
---|
[481] | 437 | my $ret = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
|
---|
[123] | 438 |
|
---|
[671] | 439 | die $dnsdb->errstr."\n" if !$ret;
|
---|
[123] | 440 |
|
---|
| 441 | return $ret;
|
---|
[119] | 442 | }
|
---|
| 443 |
|
---|
[495] | 444 | sub getRecList {
|
---|
[119] | 445 | my %args = @_;
|
---|
| 446 |
|
---|
[405] | 447 | _commoncheck(\%args);
|
---|
[123] | 448 |
|
---|
[500] | 449 | # deal gracefully with alternate calling convention for args{id}
|
---|
| 450 | $args{id} = $args{ID} if !$args{id} && $args{ID};
|
---|
| 451 | # ... and fail if we don't have one
|
---|
| 452 | die "Missing zone ID\n" if !$args{id};
|
---|
| 453 |
|
---|
[721] | 454 | # caller may not know about zone IDs. accept the zone name, but require a location if so
|
---|
| 455 | if ($args{id} !~ /^\d+$/) {
|
---|
| 456 | die "Location required to use the zone name\n" if !defined($args{location});
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[405] | 459 | # set some optional args
|
---|
[500] | 460 | $args{offset} = 0 if !$args{offset};
|
---|
[123] | 461 | ## for order, need to map input to column names
|
---|
| 462 | $args{order} = 'host' if !$args{order};
|
---|
| 463 | $args{direction} = 'ASC' if !$args{direction};
|
---|
[500] | 464 | $args{defrec} = 'n' if !$args{defrec};
|
---|
| 465 | $args{revrec} = 'n' if !$args{revrec};
|
---|
[123] | 466 |
|
---|
[500] | 467 | # convert zone name to zone ID, if needed
|
---|
| 468 | if ($args{defrec} eq 'n') {
|
---|
| 469 | if ($args{revrec} eq 'n') {
|
---|
[721] | 470 | $args{id} = $dnsdb->domainID($args{id}, $args{location}) if $args{id} !~ /^\d+$/;
|
---|
[500] | 471 | } else {
|
---|
[721] | 472 | $args{id} = $dnsdb->revID($args{id}, $args{location}) if $args{id} !~ /^\d+$/
|
---|
[500] | 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[502] | 476 | # fail if we *still* don't have a valid zone ID
|
---|
[671] | 477 | die $dnsdb->errstr."\n" if !$args{id};
|
---|
[502] | 478 |
|
---|
[500] | 479 | # and finally retrieve the records.
|
---|
[495] | 480 | my $ret = $dnsdb->getRecList(defrec => $args{defrec}, revrec => $args{revrec}, id => $args{id},
|
---|
[500] | 481 | offset => $args{offset}, nrecs => $args{nrecs}, sortby => $args{sortby},
|
---|
| 482 | sortorder => $args{sortorder}, filter => $args{filter});
|
---|
[671] | 483 | die $dnsdb->errstr."\n" if !$ret;
|
---|
[123] | 484 |
|
---|
| 485 | return $ret;
|
---|
[119] | 486 | }
|
---|
| 487 |
|
---|
[123] | 488 | sub getRecCount {
|
---|
| 489 | my %args = @_;
|
---|
[119] | 490 |
|
---|
[405] | 491 | _commoncheck(\%args);
|
---|
[123] | 492 |
|
---|
[511] | 493 | _reccheck(\%args);
|
---|
| 494 |
|
---|
[721] | 495 | # caller may not know about zone IDs. accept the zone name, but require a location if so
|
---|
| 496 | if ($args{id} !~ /^\d+$/) {
|
---|
| 497 | die "Location required to use the zone name\n" if !defined($args{location});
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[405] | 500 | # set some optional args
|
---|
| 501 | $args{nrecs} = 'all' if !$args{nrecs};
|
---|
| 502 | $args{nstart} = 0 if !$args{nstart};
|
---|
| 503 | ## for order, need to map input to column names
|
---|
| 504 | $args{order} = 'host' if !$args{order};
|
---|
| 505 | $args{direction} = 'ASC' if !$args{direction};
|
---|
| 506 |
|
---|
[721] | 507 | # convert zone name to zone ID, if needed
|
---|
| 508 | if ($args{defrec} eq 'n') {
|
---|
| 509 | if ($args{revrec} eq 'n') {
|
---|
| 510 | $args{id} = $dnsdb->domainID($args{id}, $args{location}) if $args{id} !~ /^\d+$/;
|
---|
| 511 | } else {
|
---|
| 512 | $args{id} = $dnsdb->revID($args{id}, $args{location}) if $args{id} !~ /^\d+$/
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | # fail if we *still* don't have a valid zone ID
|
---|
| 517 | die $dnsdb->errstr."\n" if !$args{id};
|
---|
| 518 |
|
---|
[666] | 519 | my $ret = $dnsdb->getRecCount(defrec => $args{defrec}, revrec => $args{revrec},
|
---|
| 520 | id => $args{id}, filter => $args{filter});
|
---|
[405] | 521 |
|
---|
[671] | 522 | die $dnsdb->errstr."\n" if !$ret;
|
---|
[405] | 523 |
|
---|
| 524 | return $ret;
|
---|
[721] | 525 | } # getRecCount()
|
---|
[123] | 526 |
|
---|
[687] | 527 | # The core sub uses references for some arguments to allow limited modification for
|
---|
| 528 | # normalization or type+zone matching/mapping/availability.
|
---|
[659] | 529 | sub rpc_addRec {
|
---|
[119] | 530 | my %args = @_;
|
---|
| 531 |
|
---|
[405] | 532 | _commoncheck(\%args, 'y');
|
---|
[123] | 533 |
|
---|
[511] | 534 | _reccheck(\%args);
|
---|
[453] | 535 | _loccheck(\%args);
|
---|
| 536 | _ttlcheck(\%args);
|
---|
[123] | 537 |
|
---|
[498] | 538 | # allow passing text types rather than DNS integer IDs
|
---|
[499] | 539 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
[498] | 540 |
|
---|
[481] | 541 | my @recargs = ($args{defrec}, $args{revrec}, $args{parent_id},
|
---|
[543] | 542 | \$args{name}, \$args{type}, \$args{address}, $args{ttl}, $args{location},
|
---|
| 543 | $args{expires}, $args{stamp});
|
---|
[426] | 544 | if ($args{type} == $DNSDB::reverse_typemap{MX} or $args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
| 545 | push @recargs, $args{distance};
|
---|
| 546 | if ($args{type} == $DNSDB::reverse_typemap{SRV}) {
|
---|
| 547 | push @recargs, $args{weight};
|
---|
| 548 | push @recargs, $args{port};
|
---|
| 549 | }
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[481] | 552 | my ($code, $msg) = $dnsdb->addRec(@recargs);
|
---|
[426] | 553 |
|
---|
[511] | 554 | die "$msg\n" if $code eq 'FAIL';
|
---|
[426] | 555 | return $msg;
|
---|
[659] | 556 | } # rpc_addRec
|
---|
[119] | 557 |
|
---|
[675] | 558 | sub rpc_updateRec {
|
---|
[119] | 559 | my %args = @_;
|
---|
| 560 |
|
---|
[405] | 561 | _commoncheck(\%args, 'y');
|
---|
[123] | 562 |
|
---|
[511] | 563 | _reccheck(\%args);
|
---|
| 564 |
|
---|
[543] | 565 | # put some caller-friendly names in their rightful DB column places
|
---|
[680] | 566 | $args{val} = $args{address} if !$args{val};
|
---|
| 567 | $args{host} = $args{name} if !$args{host};
|
---|
[543] | 568 |
|
---|
[452] | 569 | # get old line, so we can update only the bits that the caller passed to change
|
---|
[481] | 570 | my $oldrec = $dnsdb->getRecLine($args{defrec}, $args{revrec}, $args{id});
|
---|
[543] | 571 | foreach my $field (qw(host type val ttl location expires distance weight port)) {
|
---|
[452] | 572 | $args{$field} = $oldrec->{$field} if !$args{$field} && defined($oldrec->{$field});
|
---|
| 573 | }
|
---|
[543] | 574 | # stamp has special handling when blank or 0. "undefined" from the caller should mean "don't change"
|
---|
[659] | 575 | $args{stamp} = $oldrec->{stamp} if !defined($args{stamp}) && $oldrec->{stampactive};
|
---|
[452] | 576 |
|
---|
[498] | 577 | # allow passing text types rather than DNS integer IDs
|
---|
[499] | 578 | $args{type} = $DNSDB::reverse_typemap{$args{type}} if $args{type} !~ /^\d+$/;
|
---|
[498] | 579 |
|
---|
[405] | 580 | # note dist, weight, port are not required on all types; will be ignored if not needed.
|
---|
[426] | 581 | # parent_id is the "primary" zone we're updating; necessary for forward/reverse voodoo
|
---|
[481] | 582 | my ($code, $msg) = $dnsdb->updateRec($args{defrec}, $args{revrec}, $args{id}, $args{parent_id},
|
---|
[543] | 583 | \$args{host}, \$args{type}, \$args{val}, $args{ttl}, $args{location},
|
---|
| 584 | $args{expires}, $args{stamp},
|
---|
[426] | 585 | $args{distance}, $args{weight}, $args{port});
|
---|
[123] | 586 |
|
---|
[511] | 587 | die "$msg\n" if $code eq 'FAIL';
|
---|
[426] | 588 | return $msg;
|
---|
[680] | 589 | } # rpc_updateRec
|
---|
[119] | 590 |
|
---|
[710] | 591 |
|
---|
[453] | 592 | # Takes a passed CIDR block and DNS pattern; adds a new record or updates the record(s) affected
|
---|
| 593 | sub addOrUpdateRevRec {
|
---|
| 594 | my %args = @_;
|
---|
| 595 |
|
---|
| 596 | _commoncheck(\%args, 'y');
|
---|
[459] | 597 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
[453] | 598 |
|
---|
[721] | 599 | # Location required so we don't turn up unrelated zones in getZonesByCIDR().
|
---|
| 600 | # Caller should generally have some knowledge of this.
|
---|
| 601 | die "Need location\n" if !defined($args{location});
|
---|
| 602 |
|
---|
[477] | 603 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
[453] | 604 | if (scalar(@$zonelist) == 0) {
|
---|
| 605 | # enhh.... WTF?
|
---|
| 606 | } elsif (scalar(@$zonelist) == 1) {
|
---|
| 607 | # check if the single zone returned is bigger than the CIDR. if so, we can just add a record
|
---|
| 608 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
[459] | 609 | if ($zone->contains($cidr)) {
|
---|
[717] | 610 | # We need to strip the CIDR mask on IPv4 /32 or v6 /128 assignments, or we just add a new record all the time.
|
---|
| 611 | my $filt = ( $cidr->{isv6} ? ($cidr->masklen != 128 ? "$cidr" : $cidr->addr) :
|
---|
| 612 | ($cidr->masklen != 32 ? "$cidr" : $cidr->addr) );
|
---|
| 613 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y',
|
---|
[454] | 614 | id => $zonelist->[0]->{rdns_id}, filter => $filt);
|
---|
[671] | 615 | ##fixme: Figure some new magic to automerge new incoming A(AAA)+PTR requests
|
---|
| 616 | # with existing A records to prevent duplication of A(AAA) records
|
---|
[453] | 617 | if (scalar(@$reclist) == 0) {
|
---|
| 618 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
[672] | 619 | my $type = ($cidr->{isv6} ? ($cidr->masklen == 128 ? 65281 : 65284) : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
[659] | 620 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
[459] | 621 | address => "$cidr", %args);
|
---|
[453] | 622 | } else {
|
---|
[459] | 623 | my $flag = 0;
|
---|
[453] | 624 | foreach my $rec (@$reclist) {
|
---|
[454] | 625 | # pure PTR plus composite types
|
---|
| 626 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281
|
---|
| 627 | || $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
| 628 | next unless $rec->{val} eq $filt; # make sure we really update the record we want to update.
|
---|
[717] | 629 | # canonicalize the IP values so funny IPv6 short forms don't
|
---|
| 630 | # cause non-updates by not being literally string-equal
|
---|
| 631 | $rec->{val} = new NetAddr::IP $rec->{val};
|
---|
| 632 | my $tmpcidr = new NetAddr::IP $args{cidr};
|
---|
| 633 | my %newrec = (host => $args{name}, val => $tmpcidr, type => $args{type});
|
---|
[675] | 634 | rpc_updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
|
---|
[710] | 635 | parent_id => $zonelist->[0]->{rdns_id}, address => "$cidr", %args)
|
---|
| 636 | if _checkRecMod($rec, \%newrec); # and only do the update if there really is something to change
|
---|
[459] | 637 | $flag = 1;
|
---|
[453] | 638 | last; # only do one record.
|
---|
| 639 | }
|
---|
[459] | 640 | unless ($flag) {
|
---|
| 641 | # Nothing was updated, so we didn't really have a match. Add as per @$reclist==0
|
---|
| 642 | # Aren't Magic Numbers Fun? See pseudotype list in dnsadmin.
|
---|
| 643 | my $type = ($cidr->{isv6} ? 65282 : ($cidr->masklen == 32 ? 65280 : 65283) );
|
---|
[659] | 644 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
|
---|
[459] | 645 | address => "$cidr", %args);
|
---|
| 646 | }
|
---|
[453] | 647 | }
|
---|
| 648 | } else {
|
---|
| 649 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
| 650 | } # done single-zone-contains-$cidr
|
---|
| 651 | } else {
|
---|
| 652 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
| 653 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
| 654 | foreach my $zdata (@$zonelist) {
|
---|
[717] | 655 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y',
|
---|
[453] | 656 | id => $zdata->{rdns_id}, filter => $zdata->{revnet});
|
---|
| 657 | if (scalar(@$reclist) == 0) {
|
---|
| 658 | my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
[659] | 659 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
[453] | 660 | address => "$args{cidr}", %args);
|
---|
| 661 | } else {
|
---|
[717] | 662 | my $updflag = 0;
|
---|
[453] | 663 | foreach my $rec (@$reclist) {
|
---|
[454] | 664 | # only the composite and/or template types; pure PTR or nontemplate composite
|
---|
| 665 | # types are nominally impossible here.
|
---|
[453] | 666 | next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
[717] | 667 | my %newrec = (host => $args{name}, val => $zdata->{revnet}, type => $args{type});
|
---|
[678] | 668 | rpc_updateRec(defrec => 'n', revrec => 'y', id => $rec->{record_id},
|
---|
[717] | 669 | parent_id => $zdata->{rdns_id}, %args)
|
---|
| 670 | if _checkRecMod($rec, \%newrec); # and only do the update if there really is something to change
|
---|
| 671 | $updflag = 1;
|
---|
[453] | 672 | last; # only do one record.
|
---|
| 673 | }
|
---|
[717] | 674 | # catch the case of "oops, no zone-sized template record and need to add a new one",
|
---|
| 675 | # because the SOA and NS records will be returned from the getRecList() call above
|
---|
| 676 | unless ($updflag) {
|
---|
| 677 | my $type = ($cidr->{isv6} ? 65284 : 65283);
|
---|
| 678 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
| 679 | address => $zdata->{revnet}, %args);
|
---|
| 680 | }
|
---|
| 681 | } # scalar(@$reclist) != 0
|
---|
[453] | 682 | } # iterate zones within $cidr
|
---|
| 683 | } # done $cidr-contains-zones
|
---|
[676] | 684 | ##fixme: what about errors? what about warnings?
|
---|
| 685 | } # done addOrUpdateRevRec()
|
---|
[453] | 686 |
|
---|
[676] | 687 | # Update rDNS on a whole batch of IP addresses. Presented as a separate sub via RPC
|
---|
| 688 | # since RPC calls can be s...l...o...w....
|
---|
| 689 | sub updateRevSet {
|
---|
| 690 | my %args = @_;
|
---|
| 691 |
|
---|
| 692 | _commoncheck(\%args, 'y');
|
---|
| 693 |
|
---|
| 694 | my @ret;
|
---|
| 695 | # loop over passed IP/hostname pairs
|
---|
| 696 | foreach my $key (keys %args) {
|
---|
[678] | 697 | next unless $key =~ m{^host_((?:[\d.]+|[\da-f:]+)(?:/\d+)?)$};
|
---|
[676] | 698 | my $ip = $1;
|
---|
[710] | 699 | push @ret, addOrUpdateRevRec(%args, cidr => $ip, name => $args{$key});
|
---|
[676] | 700 | }
|
---|
[710] | 701 |
|
---|
| 702 | # now we check the parts of the block that didn't get passed to see if they should be deleted
|
---|
| 703 | my $block = new NetAddr::IP $args{cidr};
|
---|
[717] | 704 | if (!$block->{isv6}) {
|
---|
| 705 | foreach my $ip (@{$block->splitref(32)}) {
|
---|
| 706 | my $bare = $ip->addr;
|
---|
| 707 | next if $args{"host_$bare"};
|
---|
| 708 | delByCIDR(delforward => 1, delsubs => 0, cidr => $bare, location => $args{location},
|
---|
[710] | 709 | rpcuser => $args{rpcuser}, rpcsystem => $args{rpcsystem});
|
---|
[717] | 710 | }
|
---|
[710] | 711 | }
|
---|
| 712 |
|
---|
[676] | 713 | ##fixme: what about errors? what about warnings?
|
---|
| 714 | return \@ret;
|
---|
| 715 | } # done updateRevSet()
|
---|
| 716 |
|
---|
[680] | 717 | # Split a template record as per a passed CIDR.
|
---|
| 718 | # Requires the CIDR and the new mask length
|
---|
| 719 | sub splitTemplate {
|
---|
| 720 | my %args = @_;
|
---|
| 721 |
|
---|
| 722 | _commoncheck(\%args, 'y');
|
---|
| 723 |
|
---|
| 724 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
| 725 |
|
---|
[721] | 726 | # Location required so we don't turn up unrelated zones in getZonesByCIDR().
|
---|
| 727 | # Caller should generally have some knowledge of this.
|
---|
| 728 | die "Need location\n" if !defined($args{location});
|
---|
| 729 |
|
---|
[680] | 730 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
| 731 |
|
---|
| 732 | if (scalar(@$zonelist) == 0) {
|
---|
| 733 | # enhh.... WTF?
|
---|
| 734 |
|
---|
| 735 | } elsif (scalar(@$zonelist) == 1) {
|
---|
| 736 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
| 737 | if ($zone->contains($cidr)) {
|
---|
| 738 | # Find the first record in the reverse zone that matches the CIDR we're splitting...
|
---|
[717] | 739 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y',
|
---|
[680] | 740 | id => $zonelist->[0]->{rdns_id}, filter => $cidr, sortby => 'val', sortorder => 'DESC');
|
---|
| 741 | my $oldrec;
|
---|
| 742 | foreach my $rec (@$reclist) {
|
---|
| 743 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
| 744 | next unless $cidr->contains($reccidr); # not sure this is needed here
|
---|
| 745 | # ... and is a reverse-template type.
|
---|
| 746 | # Could arguably trim the list below to just 65282, 65283, 65284
|
---|
| 747 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
| 748 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
| 749 | # snag old record so we can copy its data
|
---|
| 750 | $oldrec = $dnsdb->getRecLine('n', 'y', $rec->{record_id});
|
---|
| 751 | last; # we've found one record that meets our criteria; Extras Are Irrelevant
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 | my @newblocks = $cidr->split($args{newmask});
|
---|
| 755 | # Change the existing record with the new CIDR
|
---|
| 756 | my $up_res = rpc_updateRec(%args, val => $newblocks[0], id => $oldrec->{record_id}, defrec => 'n', revrec => 'y');
|
---|
| 757 | my @ret;
|
---|
| 758 | # the update is assumed to have succeeded if it didn't fail.
|
---|
| 759 | ##fixme: find a way to save and return "warning" states?
|
---|
| 760 | push @ret, {block => "$newblocks[0]", code => "OK", msg => $up_res};
|
---|
| 761 | # And now add new record(s) for each of the new CIDR entries, reusing the old data
|
---|
| 762 | for (my $i = 1; $i <= $#newblocks; $i++) {
|
---|
| 763 | my $newval = "$newblocks[$i]";
|
---|
| 764 | my @recargs = ('n', 'y', $oldrec->{rdns_id}, \$oldrec->{host}, \$oldrec->{type}, \$newval,
|
---|
| 765 | $oldrec->{ttl}, $oldrec->{location}, 0, '');
|
---|
| 766 | my ($code, $msg) = $dnsdb->addRec(@recargs);
|
---|
| 767 | # Note failures here are not fatal; this should typically only ever be called by IPDB
|
---|
| 768 | push @ret, {block => "$newblocks[$i]", code => $code, msg => $up_res};
|
---|
| 769 | }
|
---|
| 770 | # return an info hash in case of warnings doing the update or add(s)
|
---|
| 771 | return \@ret;
|
---|
| 772 |
|
---|
| 773 | } else { # $cidr > $zone but we only have one zone
|
---|
| 774 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
| 775 | return "Warning: $args{cidr} is only partly represented in DNS. Check and update DNS records manually.";
|
---|
| 776 | } # done single-zone-contains-$cidr
|
---|
| 777 |
|
---|
| 778 | } else {
|
---|
| 779 | # multiple zones nominally "contain" $cidr
|
---|
| 780 | } # done $cidr-contains-zones
|
---|
| 781 |
|
---|
| 782 | } # done splitTemplate()
|
---|
| 783 |
|
---|
[681] | 784 | # Resize a template according to an old/new CIDR pair
|
---|
| 785 | # Takes the old cidr in $args{oldcidr} and the new in $args{newcidr}
|
---|
| 786 | sub resizeTemplate {
|
---|
| 787 | my %args = @_;
|
---|
| 788 |
|
---|
| 789 | _commoncheck(\%args, 'y');
|
---|
| 790 |
|
---|
| 791 | my $oldcidr = new NetAddr::IP $args{oldcidr};
|
---|
| 792 | my $newcidr = new NetAddr::IP $args{newcidr};
|
---|
| 793 | die "$oldcidr and $newcidr do not overlap"
|
---|
| 794 | unless $oldcidr->contains($newcidr) || $newcidr->contains($oldcidr);
|
---|
| 795 | $args{cidr} = $args{oldcidr};
|
---|
| 796 |
|
---|
| 797 | my $up_res;
|
---|
| 798 |
|
---|
[721] | 799 | # Location required so we don't turn up unrelated zones in getZonesByCIDR().
|
---|
| 800 | # Caller should generally have some knowledge of this.
|
---|
| 801 | die "Need location\n" if !defined($args{location});
|
---|
| 802 |
|
---|
[681] | 803 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
| 804 | if (scalar(@$zonelist) == 0) {
|
---|
| 805 | # enhh.... WTF?
|
---|
| 806 |
|
---|
| 807 | } elsif (scalar(@$zonelist) == 1) {
|
---|
| 808 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
| 809 | if ($zone->contains($oldcidr)) {
|
---|
| 810 | # Find record(s) matching the old and new CIDR
|
---|
| 811 |
|
---|
| 812 | my $sql = q(
|
---|
| 813 | SELECT record_id,host,val
|
---|
| 814 | FROM records
|
---|
| 815 | WHERE rdns_id = ?
|
---|
| 816 | AND type IN (12, 65280, 65281, 65282, 65283, 65284)
|
---|
| 817 | AND (val = ? OR val = ?)
|
---|
| 818 | ORDER BY masklen(inetlazy(val)) ASC
|
---|
| 819 | );
|
---|
| 820 | my $sth = $dnsdb->{dbh}->prepare($sql);
|
---|
| 821 | $sth->execute($zonelist->[0]->{rdns_id}, "$oldcidr", "$newcidr");
|
---|
| 822 | my $upd_id;
|
---|
| 823 | my $oldhost;
|
---|
| 824 | while (my ($recid, $host, $val) = $sth->fetchrow_array) {
|
---|
| 825 | my $tcidr = NetAddr::IP->new($val);
|
---|
| 826 | if ($tcidr == $newcidr) {
|
---|
| 827 | # Match found for new CIDR. Delete this record.
|
---|
| 828 | $up_res = $dnsdb->delRec('n', 'y', $recid);
|
---|
| 829 | } else {
|
---|
| 830 | # Update this record, then exit the loop
|
---|
| 831 | $up_res = rpc_updateRec(%args, val => $newcidr, id => $recid, defrec => 'n', revrec => 'y');
|
---|
| 832 | last;
|
---|
| 833 | }
|
---|
| 834 | # Your llama is on fire
|
---|
| 835 | }
|
---|
| 836 | $sth->finish;
|
---|
| 837 |
|
---|
| 838 | return "Template record for $oldcidr changed to $newcidr.";
|
---|
| 839 |
|
---|
| 840 | } else { # $cidr > $zone but we only have one zone
|
---|
| 841 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
| 842 | return "Warning: $args{cidr} is only partly represented in DNS. Check and update DNS records manually.";
|
---|
| 843 | } # done single-zone-contains-$cidr
|
---|
| 844 |
|
---|
| 845 | } else {
|
---|
| 846 | # multiple zones nominally "contain" $cidr
|
---|
| 847 | }
|
---|
| 848 |
|
---|
| 849 | return $up_res;
|
---|
| 850 | } # done resizeTemplate()
|
---|
| 851 |
|
---|
[682] | 852 | # Convert one or more template records to a set of individual IP records. Expands the template.
|
---|
| 853 | # Handle the case of nested templates, although the primary caller (IPDB) should not be
|
---|
| 854 | # able to generate records that would trigger that case.
|
---|
| 855 | # Accounts for existing PTR or A+PTR records same as on-export template expansion.
|
---|
| 856 | # Takes a list of templates and a bounding CIDR?
|
---|
| 857 | sub templatesToRecords {
|
---|
| 858 | my %args = @_;
|
---|
| 859 |
|
---|
| 860 | _commoncheck(\%args, 'y');
|
---|
| 861 |
|
---|
| 862 | my %iplist;
|
---|
| 863 | my @retlist;
|
---|
| 864 |
|
---|
[721] | 865 | # Location required so we don't turn up unrelated zones
|
---|
| 866 | die "Need location\n" if !defined($args{location});
|
---|
| 867 |
|
---|
| 868 | my $zsth = $dnsdb->{dbh}->prepare("SELECT rdns_id,group_id FROM revzones WHERE revnet >>= ? AND location = ?");
|
---|
[682] | 869 | # Going to assume template records with no expiry
|
---|
| 870 | # Also note IPv6 template records don't expand sanely the way v4 records do
|
---|
| 871 | my $recsth = $dnsdb->{dbh}->prepare(q(
|
---|
| 872 | SELECT record_id, domain_id, host, type, val, ttl, location
|
---|
| 873 | FROM records
|
---|
| 874 | WHERE rdns_id = ?
|
---|
| 875 | AND type IN (12, 65280, 65282, 65283)
|
---|
| 876 | AND inetlazy(val) <<= ?
|
---|
| 877 | ORDER BY masklen(inetlazy(val)) DESC
|
---|
| 878 | ));
|
---|
| 879 | my $insth = $dnsdb->{dbh}->prepare("INSERT INTO records (domain_id, rdns_id, host, type, val, ttl, location)".
|
---|
| 880 | " VALUES (?,?,?,?,?,?,?)");
|
---|
| 881 | my $delsth = $dnsdb->{dbh}->prepare("DELETE FROM records WHERE record_id = ?");
|
---|
| 882 | my %typedown = (12 => 12, 65280 => 65280, 65281 => 65281, 65282 => 12, 65283 => 65280, 65284 => 65281);
|
---|
| 883 |
|
---|
| 884 | my @checkrange;
|
---|
| 885 |
|
---|
| 886 | local $dnsdb->{dbh}->{AutoCommit} = 0;
|
---|
| 887 | local $dnsdb->{dbh}->{RaiseError} = 1;
|
---|
| 888 |
|
---|
| 889 | eval {
|
---|
| 890 | foreach my $template (@{$args{templates}}) {
|
---|
[721] | 891 | $zsth->execute($template, $args{location});
|
---|
[682] | 892 | my ($zid,$zgrp) = $zsth->fetchrow_array;
|
---|
| 893 | if (!$zid) {
|
---|
| 894 | push @retlist, {$template, "Zone not found"};
|
---|
| 895 | next;
|
---|
| 896 | }
|
---|
| 897 | $recsth->execute($zid, $template);
|
---|
| 898 | while (my ($recid, $domid, $host, $type, $val, $ttl, $loc) = $recsth->fetchrow_array) {
|
---|
| 899 | # Skip single IPs with PTR or A+PTR records
|
---|
| 900 | if ($type == 12 || $type == 65280) {
|
---|
| 901 | $iplist{"$val/32"}++;
|
---|
| 902 | next;
|
---|
| 903 | }
|
---|
| 904 | my @newips = NetAddr::IP->new($template)->split(32);
|
---|
| 905 | $type = $typedown{$type};
|
---|
| 906 | foreach my $ip (@newips) {
|
---|
| 907 | next if $iplist{$ip};
|
---|
| 908 | my $newhost = $host;
|
---|
[686] | 909 | $dnsdb->_template4_expand(\$newhost, $ip->addr);
|
---|
[682] | 910 | $insth->execute($domid, $zid, $newhost, $type, $ip->addr, $ttl, $loc);
|
---|
| 911 | $iplist{$ip}++;
|
---|
| 912 | }
|
---|
| 913 | $delsth->execute($recid);
|
---|
| 914 | $dnsdb->_log(group_id => $zgrp, domain_id => $domid, rdns_id => $zid,
|
---|
| 915 | entry => "$template converted to individual $typemap{$type} records");
|
---|
| 916 | push @retlist, "$template converted to individual records";
|
---|
| 917 | } # record fetch
|
---|
| 918 | } # foreach passed template CIDR
|
---|
| 919 |
|
---|
| 920 | $dnsdb->{dbh}->commit;
|
---|
| 921 | };
|
---|
| 922 | if ($@) {
|
---|
| 923 | die "Error converting a template record to individual records: $@";
|
---|
| 924 | }
|
---|
| 925 |
|
---|
| 926 | return \@retlist;
|
---|
| 927 |
|
---|
| 928 | } # done templatesToRecords()
|
---|
| 929 |
|
---|
[119] | 930 | sub delRec {
|
---|
| 931 | my %args = @_;
|
---|
| 932 |
|
---|
[405] | 933 | _commoncheck(\%args, 'y');
|
---|
[123] | 934 |
|
---|
[511] | 935 | _reccheck(\%args);
|
---|
| 936 |
|
---|
[687] | 937 | my ($code, $msg) = $dnsdb->delRec($args{defrec}, $args{revrec}, $args{id});
|
---|
[123] | 938 |
|
---|
[511] | 939 | die "$msg\n" if $code eq 'FAIL';
|
---|
[426] | 940 | return $msg;
|
---|
[119] | 941 | }
|
---|
| 942 |
|
---|
[459] | 943 | sub delByCIDR {
|
---|
| 944 | my %args = @_;
|
---|
| 945 |
|
---|
| 946 | _commoncheck(\%args, 'y');
|
---|
| 947 |
|
---|
[683] | 948 | # Caller may pass 'n' in delsubs. Assume it should be false/undefined
|
---|
| 949 | # unless the caller explicitly requested 'yes'
|
---|
[721] | 950 | $args{delsubs} = 0 if !$args{delsubs} || $args{delsubs} ne 'y';
|
---|
[683] | 951 |
|
---|
[687] | 952 | # Don't delete the A component of an A+PTR by default
|
---|
| 953 | $args{delforward} = 0 if !$args{delforward};
|
---|
| 954 |
|
---|
[721] | 955 | # Location required so we don't turn up unrelated zones in getZonesByCIDR().
|
---|
| 956 | die "Need location\n" if !defined($args{location});
|
---|
[717] | 957 |
|
---|
[459] | 958 | # much like addOrUpdateRevRec()
|
---|
[477] | 959 | my $zonelist = $dnsdb->getZonesByCIDR(%args);
|
---|
[459] | 960 | my $cidr = new NetAddr::IP $args{cidr};
|
---|
| 961 |
|
---|
| 962 | if (scalar(@$zonelist) == 0) {
|
---|
| 963 | # enhh.... WTF?
|
---|
| 964 | } elsif (scalar(@$zonelist) == 1) {
|
---|
| 965 |
|
---|
| 966 | # check if the single zone returned is bigger than the CIDR
|
---|
| 967 | my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
|
---|
| 968 | if ($zone->contains($cidr)) {
|
---|
| 969 | if ($args{delsubs}) {
|
---|
| 970 | # Delete ALL EVARYTHING!!one11!! in $args{cidr}
|
---|
[717] | 971 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y', id => $zonelist->[0]->{rdns_id});
|
---|
[459] | 972 | foreach my $rec (@$reclist) {
|
---|
| 973 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
| 974 | next unless $cidr->contains($reccidr);
|
---|
[460] | 975 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
| 976 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
[459] | 977 | ##fixme: multiple records, wanna wax'em all, how to report errors?
|
---|
| 978 | if ($args{delforward} ||
|
---|
| 979 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
| 980 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
[481] | 981 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
[459] | 982 | } else {
|
---|
[686] | 983 | ##fixme: AAAA+PTR?
|
---|
[481] | 984 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
[459] | 985 | }
|
---|
| 986 | }
|
---|
[460] | 987 | if ($args{parpatt} && $zone == $cidr) {
|
---|
| 988 | # Edge case; we've just gone and axed all the records in the reverse zone.
|
---|
| 989 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
[659] | 990 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id},
|
---|
[672] | 991 | type => ($zone->{isv6} ? 65284 : 65283), address => "$cidr", name => $args{parpatt}, %args);
|
---|
[460] | 992 | }
|
---|
[459] | 993 |
|
---|
| 994 | } else {
|
---|
| 995 | # Selectively delete only exact matches on $args{cidr}
|
---|
| 996 | # We need to strip the CIDR mask on IPv4 /32 assignments, or we can't find single-IP records
|
---|
[717] | 997 | my $filt = ( $cidr->{isv6} ? ($cidr->masklen != 128 ? "$cidr" : $cidr->addr) :
|
---|
| 998 | ($cidr->masklen != 32 ? "$cidr" : $cidr->addr) );
|
---|
| 999 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y', location => $args{location},
|
---|
[459] | 1000 | id => $zonelist->[0]->{rdns_id}, filter => $filt, sortby => 'val', sortorder => 'DESC');
|
---|
| 1001 | foreach my $rec (@$reclist) {
|
---|
| 1002 | my $reccidr = new NetAddr::IP $rec->{val};
|
---|
| 1003 | next unless $cidr == $reccidr;
|
---|
[460] | 1004 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
| 1005 | $rec->{type} == 65282 || $rec->{type} == 65283 ||$rec->{type} == 65284;
|
---|
[459] | 1006 | if ($args{delforward} || $rec->{type} == 12) {
|
---|
[481] | 1007 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
[511] | 1008 | die "$msg\n" if $code eq 'FAIL';
|
---|
[459] | 1009 | return $msg;
|
---|
| 1010 | } else {
|
---|
[481] | 1011 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
[671] | 1012 | die $dnsdb->errstr."\n" if !$ret;
|
---|
[459] | 1013 | return "A+PTR for $args{cidr} split and PTR removed";
|
---|
| 1014 | }
|
---|
| 1015 | } # foreach @$reclist
|
---|
| 1016 | }
|
---|
| 1017 |
|
---|
| 1018 | } else { # $cidr > $zone but we only have one zone
|
---|
| 1019 | # ebbeh? CIDR is only partly represented in DNS. This needs manual intervention.
|
---|
| 1020 | return "Warning: $args{cidr} is only partly represented in DNS. Check and remove DNS records manually.";
|
---|
| 1021 | } # done single-zone-contains-$cidr
|
---|
| 1022 |
|
---|
| 1023 | } else { # multiple zones nominally "contain" $cidr
|
---|
| 1024 | # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
|
---|
| 1025 | # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
|
---|
| 1026 | foreach my $zdata (@$zonelist) {
|
---|
[717] | 1027 | my $reclist = $dnsdb->getRecList(rpc => 1, defrec => 'n', revrec => 'y', id => $zdata->{rdns_id});
|
---|
[459] | 1028 | if (scalar(@$reclist) == 0) {
|
---|
[460] | 1029 | # nothing to do? or do we (re)add a record based on the parent?
|
---|
| 1030 | # yes, yes we do, past the close of the else
|
---|
| 1031 | # my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
|
---|
[659] | 1032 | # rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
|
---|
[460] | 1033 | # address => "$args{cidr}", %args);
|
---|
[459] | 1034 | } else {
|
---|
| 1035 | foreach my $rec (@$reclist) {
|
---|
[460] | 1036 | next unless $rec->{type} == 12 || $rec->{type} == 65280 || $rec->{type} == 65281 ||
|
---|
| 1037 | $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
|
---|
| 1038 | # Template types are only useful when attached to a reverse zone.
|
---|
| 1039 | ##fixme ..... or ARE THEY?
|
---|
| 1040 | if ($args{delforward} ||
|
---|
| 1041 | $rec->{type} == 12 || $rec->{type} == 65282 ||
|
---|
| 1042 | $rec->{type} == 65283 || $rec->{type} == 65284) {
|
---|
[481] | 1043 | my ($code,$msg) = $dnsdb->delRec('n', 'y', $rec->{record_id});
|
---|
[460] | 1044 | } else {
|
---|
[481] | 1045 | my $ret = $dnsdb->downconvert($rec->{record_id}, $DNSDB::reverse_typemap{A});
|
---|
[460] | 1046 | }
|
---|
[459] | 1047 | } # foreach @$reclist
|
---|
[460] | 1048 | } # nrecs != 0
|
---|
| 1049 | if ($args{parpatt}) {
|
---|
| 1050 | # We've just gone and axed all the records in the reverse zone.
|
---|
| 1051 | # Re-add one to match the parent if we've been given a pattern to use.
|
---|
[659] | 1052 | rpc_addRec(defrec => 'n', revrec => 'y', parent_id => $zdata->{rdns_id},
|
---|
[460] | 1053 | type => ($cidr->{isv6} ? 65284 : 65283),
|
---|
| 1054 | address => $zdata->{revnet}, name => $args{parpatt}, %args);
|
---|
[459] | 1055 | }
|
---|
| 1056 | } # iterate zones within $cidr
|
---|
| 1057 | } # done $cidr-contains-zones
|
---|
| 1058 |
|
---|
| 1059 | } # end delByCIDR()
|
---|
| 1060 |
|
---|
[684] | 1061 | # Batch-delete a set of reverse entries similar to updateRevSet
|
---|
| 1062 | sub delRevSet {
|
---|
| 1063 | my %args = @_;
|
---|
| 1064 |
|
---|
| 1065 | _commoncheck(\%args, 'y');
|
---|
| 1066 |
|
---|
| 1067 | my @ret;
|
---|
| 1068 | # loop over passed CIDRs in args{cidrlist}
|
---|
| 1069 | foreach my $cidr (split(',', $args{cidrlist})) {
|
---|
| 1070 | push @ret, delByCIDR(cidr => $cidr, %args)
|
---|
| 1071 | }
|
---|
| 1072 |
|
---|
| 1073 | return \@ret;
|
---|
| 1074 | } # end delRevSet()
|
---|
| 1075 |
|
---|
[405] | 1076 | #sub getLogCount {}
|
---|
| 1077 | #sub getLogEntries {}
|
---|
[452] | 1078 |
|
---|
| 1079 | sub getRevPattern {
|
---|
| 1080 | my %args = @_;
|
---|
| 1081 |
|
---|
| 1082 | _commoncheck(\%args, 'y');
|
---|
| 1083 |
|
---|
[700] | 1084 | return $dnsdb->getRevPattern($args{cidr}, location => $args{location}, group => $args{group});
|
---|
[452] | 1085 | }
|
---|
| 1086 |
|
---|
[673] | 1087 | sub getRevSet {
|
---|
| 1088 | my %args = @_;
|
---|
| 1089 |
|
---|
| 1090 | _commoncheck(\%args, 'y');
|
---|
| 1091 |
|
---|
[700] | 1092 | return $dnsdb->getRevSet($args{cidr}, location => $args{location}, group => $args{group});
|
---|
[673] | 1093 | }
|
---|
| 1094 |
|
---|
[506] | 1095 | sub getTypelist {
|
---|
| 1096 | my %args = @_;
|
---|
| 1097 | _commoncheck(\%args, 'y');
|
---|
[504] | 1098 |
|
---|
[506] | 1099 | $args{selected} = $reverse_typemap{A} if !$args{selected};
|
---|
| 1100 |
|
---|
| 1101 | return $dnsdb->getTypelist($args{recgroup}, $args{selected});
|
---|
| 1102 | }
|
---|
| 1103 |
|
---|
[504] | 1104 | sub getTypemap {
|
---|
| 1105 | my %args = @_;
|
---|
| 1106 | _commoncheck(\%args, 'y');
|
---|
| 1107 | return \%typemap;
|
---|
| 1108 | }
|
---|
| 1109 |
|
---|
| 1110 | sub getReverse_typemap {
|
---|
| 1111 | my %args = @_;
|
---|
| 1112 | _commoncheck(\%args, 'y');
|
---|
| 1113 | return \%reverse_typemap;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
[405] | 1116 | #sub parentID {}
|
---|
| 1117 | #sub isParent {}
|
---|
[123] | 1118 |
|
---|
[405] | 1119 | sub zoneStatus {
|
---|
[123] | 1120 | my %args = @_;
|
---|
| 1121 |
|
---|
[405] | 1122 | _commoncheck(\%args, 'y');
|
---|
[123] | 1123 |
|
---|
[687] | 1124 | $args{reverse} = 'n' if !$args{reverse} || $args{reverse} ne 'y';
|
---|
| 1125 | my @arglist = ($args{zoneid}, $args{reverse});
|
---|
[123] | 1126 | push @arglist, $args{status} if defined($args{status});
|
---|
| 1127 |
|
---|
[477] | 1128 | my $status = $dnsdb->zoneStatus(@arglist);
|
---|
[123] | 1129 | }
|
---|
| 1130 |
|
---|
[452] | 1131 | # Get a list of hashes referencing the reverse zone(s) for a passed CIDR block
|
---|
| 1132 | sub getZonesByCIDR {
|
---|
| 1133 | my %args = @_;
|
---|
| 1134 |
|
---|
| 1135 | _commoncheck(\%args, 'y');
|
---|
| 1136 |
|
---|
[477] | 1137 | return $dnsdb->getZonesByCIDR(%args);
|
---|
[452] | 1138 | }
|
---|
| 1139 |
|
---|
[405] | 1140 | #sub importAXFR {}
|
---|
| 1141 | #sub importBIND {}
|
---|
| 1142 | #sub import_tinydns {}
|
---|
| 1143 | #sub export {}
|
---|
| 1144 | #sub __export_tiny {}
|
---|
| 1145 | #sub _printrec_tiny {}
|
---|
| 1146 | #sub mailNotify {}
|
---|
[119] | 1147 |
|
---|
| 1148 | sub get_method_list {
|
---|
| 1149 | my @methods = keys %{$methods};
|
---|
| 1150 | return \@methods;
|
---|
| 1151 | }
|
---|