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