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