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