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