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