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