| 1 | # ipdb/cgi-bin/IPDB.pm
 | 
|---|
| 2 | # Contains functions for IPDB - database access, subnet mangling, block allocation, etc
 | 
|---|
| 3 | ###
 | 
|---|
| 4 | # SVN revision info
 | 
|---|
| 5 | # $Date: 2014-12-29 22:52:03 +0000 (Mon, 29 Dec 2014) $
 | 
|---|
| 6 | # SVN revision $Rev: 661 $
 | 
|---|
| 7 | # Last update by $Author: kdeugau $
 | 
|---|
| 8 | ###
 | 
|---|
| 9 | # Copyright (C) 2004-2010 - Kris Deugau
 | 
|---|
| 10 | 
 | 
|---|
| 11 | package IPDB;
 | 
|---|
| 12 | 
 | 
|---|
| 13 | use strict;
 | 
|---|
| 14 | use warnings;
 | 
|---|
| 15 | use Exporter;
 | 
|---|
| 16 | use DBI;
 | 
|---|
| 17 | use Net::SMTP;
 | 
|---|
| 18 | use NetAddr::IP qw(:lower Compact );
 | 
|---|
| 19 | use Frontier::Client;
 | 
|---|
| 20 | use POSIX;
 | 
|---|
| 21 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 | 
|---|
| 22 | 
 | 
|---|
| 23 | $VERSION        = 2; ##VERSION##
 | 
|---|
| 24 | @ISA            = qw(Exporter);
 | 
|---|
| 25 | @EXPORT_OK    = qw(
 | 
|---|
| 26 |         %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
 | 
|---|
| 27 |         %IPDBacl %aclmsg %rpcacl $maxfcgi
 | 
|---|
| 28 |         $errstr
 | 
|---|
| 29 |         &initIPDBGlobals &connectDB &finish &checkDBSanity
 | 
|---|
| 30 |         &addMaster &touchMaster
 | 
|---|
| 31 |         &listSummary &listSubs &listFree &listPool
 | 
|---|
| 32 |         &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
 | 
|---|
| 33 |         &ipParent &subParent &blockParent &getRoutedCity
 | 
|---|
| 34 |         &allocateBlock &updateBlock &deleteBlock &getBlockData &getBlockRDNS
 | 
|---|
| 35 |         &getNodeList &getNodeName &getNodeInfo
 | 
|---|
| 36 |         &mailNotify
 | 
|---|
| 37 |         );
 | 
|---|
| 38 | 
 | 
|---|
| 39 | @EXPORT         = (); # Export nothing by default.
 | 
|---|
| 40 | %EXPORT_TAGS    = ( ALL => [qw(
 | 
|---|
| 41 |                 %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist
 | 
|---|
| 42 |                 %IPDBacl %aclmsg %rpcacl $maxfcgi
 | 
|---|
| 43 |                 $errstr
 | 
|---|
| 44 |                 &initIPDBGlobals &connectDB &finish &checkDBSanity
 | 
|---|
| 45 |                 &addMaster &touchMaster
 | 
|---|
| 46 |                 &listSummary &listSubs &listFree &listPool
 | 
|---|
| 47 |                 &getMasterList &getTypeList &getPoolSelect &findAllocateFrom
 | 
|---|
| 48 |                 &ipParent &subParent &blockParent &getRoutedCity
 | 
|---|
| 49 |                 &allocateBlock &updateBlock &deleteBlock &getBlockData &getBlockRDNS
 | 
|---|
| 50 |                 &getNodeList &getNodeName &getNodeInfo
 | 
|---|
| 51 |                 &mailNotify
 | 
|---|
| 52 |                 )]
 | 
|---|
| 53 |         );
 | 
|---|
| 54 | 
 | 
|---|
| 55 | ##
 | 
|---|
| 56 | ## Global variables
 | 
|---|
| 57 | ##
 | 
|---|
| 58 | our %disp_alloctypes;
 | 
|---|
| 59 | our %list_alloctypes;
 | 
|---|
| 60 | our %def_custids;
 | 
|---|
| 61 | our @citylist;
 | 
|---|
| 62 | our @poplist;
 | 
|---|
| 63 | our %IPDBacl;
 | 
|---|
| 64 | 
 | 
|---|
| 65 | # mapping table for functional-area => error message
 | 
|---|
| 66 | our %aclmsg = (
 | 
|---|
| 67 |         addmaster       => 'add a master block',
 | 
|---|
| 68 |         addblock        => 'add an allocation',
 | 
|---|
| 69 |         updateblock     => 'update a block',
 | 
|---|
| 70 |         delblock        => 'delete an allocation',
 | 
|---|
| 71 |         );
 | 
|---|
| 72 | 
 | 
|---|
| 73 | our %rpcacl;
 | 
|---|
| 74 | our $maxfcgi = 3;
 | 
|---|
| 75 | 
 | 
|---|
| 76 | # error reporting
 | 
|---|
| 77 | our $errstr = '';
 | 
|---|
| 78 | 
 | 
|---|
| 79 | our $org_name = 'Example Corp';
 | 
|---|
| 80 | our $smtphost = 'smtp.example.com';
 | 
|---|
| 81 | our $domain = 'example.com';
 | 
|---|
| 82 | our $defcustid = '5554242';
 | 
|---|
| 83 | # mostly for rwhois
 | 
|---|
| 84 | ##fixme:  leave these blank by default?
 | 
|---|
| 85 | our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6
 | 
|---|
| 86 | our $org_street = '123 4th Street';
 | 
|---|
| 87 | our $org_city = 'Anytown';
 | 
|---|
| 88 | our $org_prov_state = 'ON';
 | 
|---|
| 89 | our $org_pocode = 'H0H 0H0';
 | 
|---|
| 90 | our $org_country = 'CA';
 | 
|---|
| 91 | our $org_phone = '000-555-1234';
 | 
|---|
| 92 | our $org_techhandle = 'ISP-ARIN-HANDLE';
 | 
|---|
| 93 | our $org_email = 'noc@example.com';
 | 
|---|
| 94 | our $hostmaster = 'dns@example.com';
 | 
|---|
| 95 | 
 | 
|---|
| 96 | our $syslog_facility = 'local2';
 | 
|---|
| 97 | 
 | 
|---|
| 98 | our $rpc_url = '';
 | 
|---|
| 99 | our $revgroup = 1;      # should probably be configurable somewhere
 | 
|---|
| 100 | our $rpccount = 0;
 | 
|---|
| 101 | 
 | 
|---|
| 102 | ##
 | 
|---|
| 103 | ## Internal utility functions
 | 
|---|
| 104 | ##
 | 
|---|
| 105 | 
 | 
|---|
| 106 | ## IPDB::_rpc
 | 
|---|
| 107 | # Make an RPC call for DNS changes
 | 
|---|
| 108 | sub _rpc {
 | 
|---|
| 109 |   return if !$rpc_url;  # Just In Case
 | 
|---|
| 110 |   my $rpcsub = shift;
 | 
|---|
| 111 |   my %args = @_;
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   # Make an object to represent the XML-RPC server.
 | 
|---|
| 114 |   my $server = Frontier::Client->new(url => $rpc_url, debug => 0);
 | 
|---|
| 115 |   my $result;
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   my %rpcargs = (
 | 
|---|
| 118 |         rpcsystem => 'ipdb',
 | 
|---|
| 119 | # must be provided by caller's caller
 | 
|---|
| 120 | #       rpcuser => $args{user},
 | 
|---|
| 121 |         %args,
 | 
|---|
| 122 |         );
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   eval {
 | 
|---|
| 125 |     $result = $server->call("dnsdb.$rpcsub", %rpcargs);
 | 
|---|
| 126 |   };
 | 
|---|
| 127 |   if ($@) {
 | 
|---|
| 128 |     $errstr = $@;
 | 
|---|
| 129 |     $errstr =~ s/Fault returned from XML RPC Server, fault code 4: error executing RPC `dnsdb.$rpcsub'\.\s//;
 | 
|---|
| 130 |   }
 | 
|---|
| 131 |   $rpccount++;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   return $result if $result;
 | 
|---|
| 134 | } # end _rpc()
 | 
|---|
| 135 | 
 | 
|---|
| 136 | 
 | 
|---|
| 137 | # Let's initialize the globals.
 | 
|---|
| 138 | ## IPDB::initIPDBGlobals()
 | 
|---|
| 139 | # Initialize all globals.  Takes a database handle, returns a success or error code
 | 
|---|
| 140 | sub initIPDBGlobals {
 | 
|---|
| 141 |   my $dbh = $_[0];
 | 
|---|
| 142 |   my $sth;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   # Initialize alloctypes hashes
 | 
|---|
| 145 |   $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder");
 | 
|---|
| 146 |   $sth->execute;
 | 
|---|
| 147 |   while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 148 |     $disp_alloctypes{$data[0]} = $data[2];
 | 
|---|
| 149 |     $def_custids{$data[0]} = $data[4];
 | 
|---|
| 150 |     if ($data[3] < 900) {
 | 
|---|
| 151 |       $list_alloctypes{$data[0]} = $data[1];
 | 
|---|
| 152 |     }
 | 
|---|
| 153 |   }
 | 
|---|
| 154 | 
 | 
|---|
| 155 |   # City and POP listings
 | 
|---|
| 156 |   $sth = $dbh->prepare("select city,routing from cities order by city");
 | 
|---|
| 157 |   $sth->execute;
 | 
|---|
| 158 |   return (undef,$sth->errstr) if $sth->err;
 | 
|---|
| 159 |   while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 160 |     push @citylist, $data[0];
 | 
|---|
| 161 |     if ($data[1] eq 'y') {
 | 
|---|
| 162 |       push @poplist, $data[0];
 | 
|---|
| 163 |     }
 | 
|---|
| 164 |   }
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   # Load ACL data.  Specific username checks are done at a different level.
 | 
|---|
| 167 |   $sth = $dbh->prepare("select username,acl from users");
 | 
|---|
| 168 |   $sth->execute;
 | 
|---|
| 169 |   return (undef,$sth->errstr) if $sth->err;
 | 
|---|
| 170 |   while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 171 |     $IPDBacl{$data[0]} = $data[1];
 | 
|---|
| 172 |   }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | ##fixme:  initialize HTML::Template env var for template path
 | 
|---|
| 175 | # something like $self->path().'/templates' ?
 | 
|---|
| 176 | #  $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar';
 | 
|---|
| 177 | 
 | 
|---|
| 178 |   return (1,"OK");
 | 
|---|
| 179 | } # end initIPDBGlobals
 | 
|---|
| 180 | 
 | 
|---|
| 181 | 
 | 
|---|
| 182 | ## IPDB::connectDB()
 | 
|---|
| 183 | # Creates connection to IPDB.
 | 
|---|
| 184 | # Requires the database name, username, and password.
 | 
|---|
| 185 | # Returns a handle to the db.
 | 
|---|
| 186 | # Set up for a PostgreSQL db;  could be any transactional DBMS with the
 | 
|---|
| 187 | # right changes.
 | 
|---|
| 188 | sub connectDB {
 | 
|---|
| 189 |   my $dbname = shift;
 | 
|---|
| 190 |   my $user = shift;
 | 
|---|
| 191 |   my $pass = shift;
 | 
|---|
| 192 |   my $dbhost = shift;
 | 
|---|
| 193 | 
 | 
|---|
| 194 |   my $dbh;
 | 
|---|
| 195 |   my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname";
 | 
|---|
| 196 | 
 | 
|---|
| 197 | # Note that we want to autocommit by default, and we will turn it off locally as necessary.
 | 
|---|
| 198 | # We may not want to print gobbledygook errors;  YMMV.  Have to ponder that further.
 | 
|---|
| 199 |   $dbh = DBI->connect($DSN, $user, $pass, {
 | 
|---|
| 200 |         AutoCommit => 1,
 | 
|---|
| 201 |         PrintError => 0
 | 
|---|
| 202 |         })
 | 
|---|
| 203 |     or return (undef, $DBI::errstr) if(!$dbh);
 | 
|---|
| 204 | 
 | 
|---|
| 205 | # Return here if we can't select.  Note that this indicates a
 | 
|---|
| 206 | # problem executing the select.
 | 
|---|
| 207 |   my $sth = $dbh->prepare("select type from alloctypes");
 | 
|---|
| 208 |   $sth->execute();
 | 
|---|
| 209 |   return (undef,$DBI::errstr) if ($sth->err);
 | 
|---|
| 210 | 
 | 
|---|
| 211 | # See if the select returned anything (or null data).  This should
 | 
|---|
| 212 | # succeed if the select executed, but...
 | 
|---|
| 213 |   $sth->fetchrow();
 | 
|---|
| 214 |   return (undef,$DBI::errstr)  if ($sth->err);
 | 
|---|
| 215 | 
 | 
|---|
| 216 | # If we get here, we should be OK.
 | 
|---|
| 217 |   return ($dbh,"DB connection OK");
 | 
|---|
| 218 | } # end connectDB
 | 
|---|
| 219 | 
 | 
|---|
| 220 | 
 | 
|---|
| 221 | ## IPDB::finish()
 | 
|---|
| 222 | # Cleans up after database handles and so on.
 | 
|---|
| 223 | # Requires a database handle
 | 
|---|
| 224 | sub finish {
 | 
|---|
| 225 |   my $dbh = $_[0];
 | 
|---|
| 226 |   $dbh->disconnect if $dbh;
 | 
|---|
| 227 | } # end finish
 | 
|---|
| 228 | 
 | 
|---|
| 229 | 
 | 
|---|
| 230 | ## IPDB::checkDBSanity()
 | 
|---|
| 231 | # Quick check to see if the db is responding.  A full integrity
 | 
|---|
| 232 | # check will have to be a separate tool to walk the IP allocation trees.
 | 
|---|
| 233 | sub checkDBSanity {
 | 
|---|
| 234 |   my ($dbh) = $_[0];
 | 
|---|
| 235 | 
 | 
|---|
| 236 |   if (!$dbh) {
 | 
|---|
| 237 |     print "No database handle, or connection has been closed.";
 | 
|---|
| 238 |     return -1;
 | 
|---|
| 239 |   } else {
 | 
|---|
| 240 |     # it connects, try a stmt.
 | 
|---|
| 241 |     my $sth = $dbh->prepare("select type from alloctypes");
 | 
|---|
| 242 |     my $err = $sth->execute();
 | 
|---|
| 243 | 
 | 
|---|
| 244 |     if ($sth->fetchrow()) {
 | 
|---|
| 245 |       # all is well.
 | 
|---|
| 246 |       return 1;
 | 
|---|
| 247 |     } else {
 | 
|---|
| 248 |       print "Connected to the database, but could not execute test statement.  ".$sth->errstr();
 | 
|---|
| 249 |       return -1;
 | 
|---|
| 250 |     }
 | 
|---|
| 251 |   }
 | 
|---|
| 252 |   # Clean up after ourselves.
 | 
|---|
| 253 | #  $dbh->disconnect;
 | 
|---|
| 254 | } # end checkDBSanity
 | 
|---|
| 255 | 
 | 
|---|
| 256 | 
 | 
|---|
| 257 | ## IPDB::addMaster()
 | 
|---|
| 258 | # Does all the magic necessary to sucessfully add a master block
 | 
|---|
| 259 | # Requires database handle, block to add
 | 
|---|
| 260 | # Returns failure code and error message or success code and "message"
 | 
|---|
| 261 | sub addMaster {
 | 
|---|
| 262 |   my $dbh = shift;
 | 
|---|
| 263 |         # warning!  during testing, this somehow generated a "Bad file descriptor" error.  O_o
 | 
|---|
| 264 |   my $cidr = new NetAddr::IP shift;
 | 
|---|
| 265 |   my %args = @_;
 | 
|---|
| 266 | 
 | 
|---|
| 267 |   $args{vrf} = '' if !$args{vrf};
 | 
|---|
| 268 |   $args{rdns} = '' if !$args{rdns};
 | 
|---|
| 269 |   $args{defloc} = '' if !$args{defloc};
 | 
|---|
| 270 |   $args{rwhois} = 'n' if !$args{rwhois};        # fail "safe", sort of.
 | 
|---|
| 271 |   $args{rwhois} = 'n' if $args{rwhois} ne 'n' and $args{rwhois} ne 'y';
 | 
|---|
| 272 | 
 | 
|---|
| 273 |   my $mid;
 | 
|---|
| 274 | 
 | 
|---|
| 275 |   # Allow transactions, and raise an exception on errors so we can catch it later.
 | 
|---|
| 276 |   # Use local to make sure these get "reset" properly on exiting this block
 | 
|---|
| 277 |   local $dbh->{AutoCommit} = 0;
 | 
|---|
| 278 |   local $dbh->{RaiseError} = 1;
 | 
|---|
| 279 | 
 | 
|---|
| 280 |   # Wrap all the SQL in a transaction
 | 
|---|
| 281 |   eval {
 | 
|---|
| 282 |     # First check - does the master exist?  Ignore VRFs until we can see a sane UI
 | 
|---|
| 283 |     my ($mcontained) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr >>= ? AND type = 'mm'",
 | 
|---|
| 284 |         undef, ($cidr) );
 | 
|---|
| 285 |     die "Master block $mcontained already exists and entirely contains $cidr\n"
 | 
|---|
| 286 |         if $mcontained;
 | 
|---|
| 287 | 
 | 
|---|
| 288 |     # Second check - does the new master contain an existing one or ones?
 | 
|---|
| 289 |     my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM allocations WHERE cidr <<= ? AND type = 'mm'",
 | 
|---|
| 290 |         undef, ($cidr) );
 | 
|---|
| 291 | 
 | 
|---|
| 292 |     if (!$mexist) {
 | 
|---|
| 293 |       # First case - master is brand-spanking-new.
 | 
|---|
| 294 | ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things
 | 
|---|
| 295 | ## maybe a db table called "config"?
 | 
|---|
| 296 |       $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
 | 
|---|
| 297 |         ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
 | 
|---|
| 298 |       ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 | 
|---|
| 299 | 
 | 
|---|
| 300 | # Unrouted blocks aren't associated with a city (yet).  We don't rely on this
 | 
|---|
| 301 | # elsewhere though;  legacy data may have traps and pitfalls in it to break this.
 | 
|---|
| 302 | # Thus the "routed" flag.
 | 
|---|
| 303 |       $dbh->do("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id) VALUES (?,?,?,?,?,?)", undef,
 | 
|---|
| 304 |         ($cidr, '<NULL>', 'm', $mid, $args{vrf}, $mid) );
 | 
|---|
| 305 | 
 | 
|---|
| 306 |       # master should be its own master, so deletes directly at the master level work
 | 
|---|
| 307 |       $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
 | 
|---|
| 308 | 
 | 
|---|
| 309 |       # If we get here, everything is happy.  Commit changes.
 | 
|---|
| 310 |       $dbh->commit;
 | 
|---|
| 311 | 
 | 
|---|
| 312 |     } # done new master does not contain existing master(s)
 | 
|---|
| 313 |     else {
 | 
|---|
| 314 | 
 | 
|---|
| 315 |       # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it.
 | 
|---|
| 316 |       my $smallmask = $cidr->masklen;
 | 
|---|
| 317 |       my $sth = $dbh->prepare("SELECT cidr,id FROM allocations WHERE cidr <<= ? AND type='mm' AND parent_id=0");
 | 
|---|
| 318 |       $sth->execute($cidr);
 | 
|---|
| 319 |       my @cmasters;
 | 
|---|
| 320 |       my @oldmids;
 | 
|---|
| 321 |       while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 322 |         my $master = new NetAddr::IP $data[0];
 | 
|---|
| 323 |         push @cmasters, $master;
 | 
|---|
| 324 |         push @oldmids, $data[1];
 | 
|---|
| 325 |         $smallmask = $master->masklen if $master->masklen > $smallmask;
 | 
|---|
| 326 |       }
 | 
|---|
| 327 | 
 | 
|---|
| 328 |       # split the new master, and keep only those blocks not part of an existing master
 | 
|---|
| 329 |       my @blocklist;
 | 
|---|
| 330 |       foreach my $seg ($cidr->split($smallmask)) {
 | 
|---|
| 331 |         my $contained = 0;
 | 
|---|
| 332 |         foreach my $master (@cmasters) {
 | 
|---|
| 333 |           $contained = 1 if $master->contains($seg);
 | 
|---|
| 334 |         }
 | 
|---|
| 335 |         push @blocklist, $seg if !$contained;
 | 
|---|
| 336 |       }
 | 
|---|
| 337 | 
 | 
|---|
| 338 | ##fixme:  master_id
 | 
|---|
| 339 |       # collect the unrouted free blocks within the new master
 | 
|---|
| 340 |       $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE masklen(cidr) <= ? AND cidr <<= ? AND routed = 'm'");
 | 
|---|
| 341 |       $sth->execute($smallmask, $cidr);
 | 
|---|
| 342 |       while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 343 |         my $freeblock = new NetAddr::IP $data[0];
 | 
|---|
| 344 |         push @blocklist, $freeblock;
 | 
|---|
| 345 |       }
 | 
|---|
| 346 | 
 | 
|---|
| 347 |       # combine the set of free blocks we should have now.
 | 
|---|
| 348 |       @blocklist = Compact(@blocklist);
 | 
|---|
| 349 | 
 | 
|---|
| 350 |       # master
 | 
|---|
| 351 |       $dbh->do("INSERT INTO allocations (cidr,type,swip,vrf,rdns) VALUES (?,?,?,?,?)", undef,
 | 
|---|
| 352 |         ($cidr, 'mm', 'y', $args{vrf}, $args{rdns}) );
 | 
|---|
| 353 |       ($mid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 | 
|---|
| 354 | 
 | 
|---|
| 355 |       # master should be its own master, so deletes directly at the master level work
 | 
|---|
| 356 |       $dbh->do("UPDATE allocations SET master_id = ? WHERE id = ?", undef, ($mid, $mid) );
 | 
|---|
| 357 | 
 | 
|---|
| 358 |       # and now insert the new data.  Make sure to delete old masters too.
 | 
|---|
| 359 | 
 | 
|---|
| 360 |       # freeblocks
 | 
|---|
| 361 |       $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ? AND parent_id IN (".join(',', @oldmids).")");
 | 
|---|
| 362 |       my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,vrf,master_id)".
 | 
|---|
| 363 |         " VALUES (?,'<NULL>','m',?,?,?)");
 | 
|---|
| 364 |       foreach my $newblock (@blocklist) {
 | 
|---|
| 365 |         $sth->execute($newblock);
 | 
|---|
| 366 |         $sth2->execute($newblock, $mid, $args{vrf}, $mid);
 | 
|---|
| 367 |       }
 | 
|---|
| 368 | 
 | 
|---|
| 369 |       # Update immediate allocations, and remove the old parents
 | 
|---|
| 370 |       $sth = $dbh->prepare("UPDATE allocations SET parent_id = ? WHERE parent_id = ?");
 | 
|---|
| 371 |       $sth2 = $dbh->prepare("DELETE FROM allocations WHERE id = ?");
 | 
|---|
| 372 |       foreach my $old (@oldmids) {
 | 
|---|
| 373 |         $sth->execute($mid, $old);
 | 
|---|
| 374 |         $sth2->execute($old);
 | 
|---|
| 375 |       }
 | 
|---|
| 376 | 
 | 
|---|
| 377 |       # *whew*  If we got here, we likely suceeded.
 | 
|---|
| 378 |       $dbh->commit;
 | 
|---|
| 379 | 
 | 
|---|
| 380 |     } # new master contained existing master(s)
 | 
|---|
| 381 |   }; # end eval
 | 
|---|
| 382 | 
 | 
|---|
| 383 |   if ($@) {
 | 
|---|
| 384 |     my $msg = $@;
 | 
|---|
| 385 |     eval { $dbh->rollback; };
 | 
|---|
| 386 |     return ('FAIL',$msg);
 | 
|---|
| 387 |   } else {
 | 
|---|
| 388 | 
 | 
|---|
| 389 |     # Only attempt rDNS if the IPDB side succeeded
 | 
|---|
| 390 |     if ($rpc_url) {
 | 
|---|
| 391 | 
 | 
|---|
| 392 | # Note *not* splitting reverse zones negates any benefit from caching the exported data.
 | 
|---|
| 393 | # IPv6 address space is far too large to split usefully, and in any case (also due to
 | 
|---|
| 394 | # the large address space) doesn't support the iterated template records v4 zones do
 | 
|---|
| 395 | # that causes the bulk of the slowdown that needs the cache anyway.
 | 
|---|
| 396 | 
 | 
|---|
| 397 |       my @zonelist;
 | 
|---|
| 398 | # allow splitting reverse zones to be disabled, maybe, someday
 | 
|---|
| 399 | #if ($splitrevzones && !$cidr->{isv6}) {
 | 
|---|
| 400 |       if (1 && !$cidr->{isv6}) {
 | 
|---|
| 401 |         my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24);      # hack pthui
 | 
|---|
| 402 |         @zonelist = $cidr->split($splitpoint);
 | 
|---|
| 403 |       } else {
 | 
|---|
| 404 |         @zonelist = ($cidr);
 | 
|---|
| 405 |       }
 | 
|---|
| 406 |       my @fails;
 | 
|---|
| 407 |       ##fixme:  remove hardcoding where possible
 | 
|---|
| 408 |       foreach my $subzone (@zonelist) {
 | 
|---|
| 409 |         my %rpcargs = (
 | 
|---|
| 410 |           rpcuser => $args{user},
 | 
|---|
| 411 |           revzone => "$subzone",
 | 
|---|
| 412 |           revpatt => $args{rdns},
 | 
|---|
| 413 |           defloc => $args{defloc},
 | 
|---|
| 414 |           group => $revgroup,   # not sure how these two could sanely be exposed, tbh...
 | 
|---|
| 415 |           state => 1,   # could make them globally configurable maybe
 | 
|---|
| 416 |         );
 | 
|---|
| 417 |         if ($rpc_url && !_rpc('addRDNS', %rpcargs)) {
 | 
|---|
| 418 |           push @fails, ("$subzone" => $errstr);
 | 
|---|
| 419 |         }
 | 
|---|
| 420 |       }
 | 
|---|
| 421 |       if (@fails) {
 | 
|---|
| 422 |         $errstr = "Warning(s) adding $cidr to reverse DNS:\n".join("\n", @fails);
 | 
|---|
| 423 |         return ('WARN',$mid);
 | 
|---|
| 424 |       }
 | 
|---|
| 425 |     }
 | 
|---|
| 426 |     return ('OK',$mid);
 | 
|---|
| 427 |   }
 | 
|---|
| 428 | } # end addMaster
 | 
|---|
| 429 | 
 | 
|---|
| 430 | 
 | 
|---|
| 431 | ## IPDB::touchMaster()
 | 
|---|
| 432 | # Update last-changed timestamp on a master block.
 | 
|---|
| 433 | sub touchMaster {
 | 
|---|
| 434 |   my $dbh = shift; 
 | 
|---|
| 435 |   my $master = shift;
 | 
|---|
| 436 | 
 | 
|---|
| 437 |   local $dbh->{AutoCommit} = 0;
 | 
|---|
| 438 |   local $dbh->{RaiseError} = 1;
 | 
|---|
| 439 | 
 | 
|---|
| 440 |   eval {
 | 
|---|
| 441 |     $dbh->do("UPDATE allocations SET modifystamp=now() WHERE id = ?", undef, ($master));
 | 
|---|
| 442 |     $dbh->commit;
 | 
|---|
| 443 |   };
 | 
|---|
| 444 | 
 | 
|---|
| 445 |   if ($@) {
 | 
|---|
| 446 |     my $msg = $@;
 | 
|---|
| 447 |     eval { $dbh->rollback; };
 | 
|---|
| 448 |     return ('FAIL',$msg);
 | 
|---|
| 449 |   }
 | 
|---|
| 450 |   return ('OK','OK');
 | 
|---|
| 451 | } # end touchMaster()
 | 
|---|
| 452 | 
 | 
|---|
| 453 | 
 | 
|---|
| 454 | ## IPDB::listSummary()
 | 
|---|
| 455 | # Get summary list of all master blocks
 | 
|---|
| 456 | # Returns an arrayref to a list of hashrefs containing the master block, routed count,
 | 
|---|
| 457 | # allocated count, free count, and largest free block masklength
 | 
|---|
| 458 | sub listSummary {
 | 
|---|
| 459 |   my $dbh = shift;
 | 
|---|
| 460 | 
 | 
|---|
| 461 |   my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master,id,vrf FROM allocations ".
 | 
|---|
| 462 |         "WHERE type='mm' ORDER BY cidr",
 | 
|---|
| 463 |         { Slice => {} });
 | 
|---|
| 464 | 
 | 
|---|
| 465 |   foreach (@{$mlist}) {
 | 
|---|
| 466 |     my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ?",
 | 
|---|
| 467 |         undef, ($$_{master}, $$_{id}));
 | 
|---|
| 468 |     $$_{routed} = $rcnt;
 | 
|---|
| 469 |     my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
 | 
|---|
| 470 |         "AND NOT type='rm' AND NOT type='mm' AND master_id = ?",
 | 
|---|
| 471 |         undef, ($$_{master}, $$_{id}));
 | 
|---|
| 472 |     $$_{allocated} = $acnt;
 | 
|---|
| 473 |     my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?",
 | 
|---|
| 474 |         undef, ($$_{master}, $$_{id}));
 | 
|---|
| 475 |     $$_{free} = $fcnt;
 | 
|---|
| 476 |     my ($bigfree) = $dbh->selectrow_array("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
 | 
|---|
| 477 |         " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1", undef, ($$_{master}, $$_{id}));
 | 
|---|
| 478 | ##fixme:  should find a way to do this without having to HTMLize the <>
 | 
|---|
| 479 |     $bigfree = "/$bigfree" if $bigfree;
 | 
|---|
| 480 |     $bigfree = '<NONE>' if !$bigfree;
 | 
|---|
| 481 |     $$_{bigfree} = $bigfree;
 | 
|---|
| 482 |   }
 | 
|---|
| 483 |   return $mlist;
 | 
|---|
| 484 | } # end listSummary()
 | 
|---|
| 485 | 
 | 
|---|
| 486 | 
 | 
|---|
| 487 | ## IPDB::listSubs()
 | 
|---|
| 488 | # Get list of subnets within a specified CIDR block, on a specified VRF.
 | 
|---|
| 489 | # Returns an arrayref to a list of hashrefs containing the CIDR block, customer location or
 | 
|---|
| 490 | # city it's routed to, block type, SWIP status, and description
 | 
|---|
| 491 | sub listSubs {
 | 
|---|
| 492 |   my $dbh = shift;
 | 
|---|
| 493 |   my %args = @_;
 | 
|---|
| 494 | 
 | 
|---|
| 495 |   # Just In Case
 | 
|---|
| 496 |   $args{vrf} = '' if !$args{vrf};
 | 
|---|
| 497 | 
 | 
|---|
| 498 |   # Snag the allocations for this block
 | 
|---|
| 499 |   my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,id,master_id".
 | 
|---|
| 500 |         " FROM allocations WHERE parent_id = ? ORDER BY cidr");
 | 
|---|
| 501 |   $sth->execute($args{parent});
 | 
|---|
| 502 | 
 | 
|---|
| 503 |   # hack hack hack
 | 
|---|
| 504 |   # set up to flag swip=y records if they don't actually have supporting data in the customers table
 | 
|---|
| 505 |   my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
 | 
|---|
| 506 | 
 | 
|---|
| 507 |   # snag some more details
 | 
|---|
| 508 |   my $substh = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? AND type='rm' AND master_id = ? AND NOT cidr = ? ");
 | 
|---|
| 509 |   my $alsth = $dbh->prepare("SELECT count(*) FROM allocations WHERE cidr <<= ? ".
 | 
|---|
| 510 |         "AND NOT type='rm' AND NOT type='mm' AND master_id = ?");
 | 
|---|
| 511 |   my $freesth = $dbh->prepare("SELECT count(*) FROM freeblocks WHERE cidr <<= ? AND master_id = ?");
 | 
|---|
| 512 |   my $lfreesth = $dbh->prepare("SELECT masklen(cidr) AS maskbits FROM freeblocks WHERE cidr <<= ?".
 | 
|---|
| 513 |         " AND master_id = ? ORDER BY masklen(cidr) LIMIT 1");
 | 
|---|
| 514 | 
 | 
|---|
| 515 |   my @blocklist;
 | 
|---|
| 516 |   while (my ($cidr,$city,$type,$custid,$swip,$desc,$id,$mid) = $sth->fetchrow_array()) {
 | 
|---|
| 517 |     $custsth->execute($custid);
 | 
|---|
| 518 |     my ($ncust) = $custsth->fetchrow_array();
 | 
|---|
| 519 |     $substh->execute($cidr, $mid, $cidr);
 | 
|---|
| 520 |     my ($cont) = $substh->fetchrow_array();
 | 
|---|
| 521 |     $alsth->execute($cidr, $mid);
 | 
|---|
| 522 |     my ($alloc) = $alsth->fetchrow_array();
 | 
|---|
| 523 |     $freesth->execute($cidr, $mid);
 | 
|---|
| 524 |     my ($free) = $freesth->fetchrow_array();
 | 
|---|
| 525 |     $lfreesth->execute($cidr, $mid);
 | 
|---|
| 526 |     my ($lfree) = $lfreesth->fetchrow_array();
 | 
|---|
| 527 |     $lfree = "/$lfree" if $lfree;
 | 
|---|
| 528 |     $lfree = '<NONE>' if !$lfree;
 | 
|---|
| 529 |     my %row = (
 | 
|---|
| 530 |         block => $cidr,
 | 
|---|
| 531 |         subcontainers => $cont,
 | 
|---|
| 532 |         suballocs => $alloc,
 | 
|---|
| 533 |         subfree => $free,
 | 
|---|
| 534 |         lfree => $lfree,
 | 
|---|
| 535 |         city => $city,
 | 
|---|
| 536 |         type => $disp_alloctypes{$type},
 | 
|---|
| 537 |         custid => $custid,
 | 
|---|
| 538 |         swip => ($swip eq 'y' ? 'Yes' : 'No'),
 | 
|---|
| 539 |         partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
 | 
|---|
| 540 |         desc => $desc,
 | 
|---|
| 541 |         hassubs => ($type eq 'rm' || $type =~ /.c/ ? 1 : 0),
 | 
|---|
| 542 |         id => $id,
 | 
|---|
| 543 |         );
 | 
|---|
| 544 | #    $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration...
 | 
|---|
| 545 |     $row{listpool} = ($type =~ /^.[pd]$/);
 | 
|---|
| 546 |     push (@blocklist, \%row);
 | 
|---|
| 547 |   }
 | 
|---|
| 548 |   return \@blocklist;
 | 
|---|
| 549 | } # end listSubs()
 | 
|---|
| 550 | 
 | 
|---|
| 551 | 
 | 
|---|
| 552 | ## IPDB::listMaster()
 | 
|---|
| 553 | # Get list of routed blocks in the requested master
 | 
|---|
| 554 | # Returns an arrayref to a list of hashrefs containing the routed block, POP/city the block is routed to,
 | 
|---|
| 555 | # allocated count, free count, and largest free block masklength
 | 
|---|
| 556 | sub listMaster {
 | 
|---|
| 557 |   my $dbh = shift;
 | 
|---|
| 558 |   my $master = shift;
 | 
|---|
| 559 | 
 | 
|---|
| 560 |   my $rlist = $dbh->selectall_arrayref("SELECT cidr AS block,city FROM routed WHERE cidr <<= ? ORDER BY cidr",
 | 
|---|
| 561 |         { Slice => {} }, ($master) );
 | 
|---|
| 562 | 
 | 
|---|
| 563 |   foreach (@{$rlist}) {
 | 
|---|
| 564 |     my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ?", undef, ($$_{block}));
 | 
|---|
| 565 |     $$_{nsubs} = $acnt;
 | 
|---|
| 566 |     my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?".
 | 
|---|
| 567 |         " AND (routed='y' OR routed='n')", undef, ($$_{block}));
 | 
|---|
| 568 |     $$_{nfree} = $fcnt;
 | 
|---|
| 569 |     my ($bigfree) = $dbh->selectrow_array("SELECT maskbits FROM freeblocks WHERE cidr <<= ?".
 | 
|---|
| 570 |         " AND (routed='y' OR routed='n') ORDER BY maskbits LIMIT 1", undef, ($$_{block}));
 | 
|---|
| 571 | ##fixme:  should find a way to do this without having to HTMLize the <>
 | 
|---|
| 572 |     $bigfree = "/$bigfree" if $bigfree;
 | 
|---|
| 573 |     $bigfree = '<NONE>' if !$bigfree;
 | 
|---|
| 574 |     $$_{lfree} = $bigfree;
 | 
|---|
| 575 |   }
 | 
|---|
| 576 |   return $rlist;
 | 
|---|
| 577 | } # end listMaster()
 | 
|---|
| 578 | 
 | 
|---|
| 579 | 
 | 
|---|
| 580 | ## IPDB::listRBlock()
 | 
|---|
| 581 | # Gets a list of free blocks in the requested parent/master in both CIDR and range notation
 | 
|---|
| 582 | # Takes a parent/master and an optional flag to look at routed or unrouted blocks, depending
 | 
|---|
| 583 | # on whether the master is a direct master or a routed block
 | 
|---|
| 584 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
 | 
|---|
| 585 | sub listRBlock {
 | 
|---|
| 586 |   my $dbh = shift;
 | 
|---|
| 587 |   my $routed = shift;
 | 
|---|
| 588 | 
 | 
|---|
| 589 |   # Snag the allocations for this block
 | 
|---|
| 590 |   my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description".
 | 
|---|
| 591 |         " FROM allocations WHERE cidr <<= ? ORDER BY cidr");
 | 
|---|
| 592 |   $sth->execute($routed);
 | 
|---|
| 593 | 
 | 
|---|
| 594 |   # hack hack hack
 | 
|---|
| 595 |   # set up to flag swip=y records if they don't actually have supporting data in the customers table
 | 
|---|
| 596 |   my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?");
 | 
|---|
| 597 | 
 | 
|---|
| 598 |   my @blocklist;
 | 
|---|
| 599 |   while (my ($cidr,$city,$type,$custid,$swip,$desc) = $sth->fetchrow_array()) {
 | 
|---|
| 600 |     $custsth->execute($custid);
 | 
|---|
| 601 |     my ($ncust) = $custsth->fetchrow_array();
 | 
|---|
| 602 |     my %row = (
 | 
|---|
| 603 |         block => $cidr,
 | 
|---|
| 604 |         city => $city,
 | 
|---|
| 605 |         type => $disp_alloctypes{$type},
 | 
|---|
| 606 |         custid => $custid,
 | 
|---|
| 607 |         swip => ($swip eq 'y' ? 'Yes' : 'No'),
 | 
|---|
| 608 |         partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0),
 | 
|---|
| 609 |         desc => $desc
 | 
|---|
| 610 |         );
 | 
|---|
| 611 |     $row{subblock} = ($type =~ /^.r$/);         # hmf.  wonder why these won't work in the hash declaration...
 | 
|---|
| 612 |     $row{listpool} = ($type =~ /^.[pd]$/);
 | 
|---|
| 613 |     push (@blocklist, \%row);
 | 
|---|
| 614 |   }
 | 
|---|
| 615 |   return \@blocklist;
 | 
|---|
| 616 | } # end listRBlock()
 | 
|---|
| 617 | 
 | 
|---|
| 618 | 
 | 
|---|
| 619 | ## IPDB::listFree()
 | 
|---|
| 620 | # Gets a list of free blocks in the requested parent/master and VRF instance in both CIDR and range notation
 | 
|---|
| 621 | # Takes a parent/master ID and an optional VRF specifier that defaults to empty.
 | 
|---|
| 622 | # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks
 | 
|---|
| 623 | # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes
 | 
|---|
| 624 | sub listFree {
 | 
|---|
| 625 |   my $dbh = shift;
 | 
|---|
| 626 | 
 | 
|---|
| 627 |   my %args = @_;
 | 
|---|
| 628 |   # Just In Case
 | 
|---|
| 629 |   $args{vrf} = '' if !$args{vrf};
 | 
|---|
| 630 | 
 | 
|---|
| 631 |   my $sth = $dbh->prepare("SELECT cidr,id FROM freeblocks WHERE parent_id = ? ORDER BY cidr");
 | 
|---|
| 632 | #  $sth->execute($args{parent}, $args{vrf});
 | 
|---|
| 633 |   $sth->execute($args{parent});
 | 
|---|
| 634 |   my @flist;
 | 
|---|
| 635 |   while (my ($cidr,$id) = $sth->fetchrow_array()) {
 | 
|---|
| 636 |     $cidr = new NetAddr::IP $cidr;
 | 
|---|
| 637 |     my %row = (
 | 
|---|
| 638 |         fblock => "$cidr",
 | 
|---|
| 639 |         frange => $cidr->range,
 | 
|---|
| 640 |         fbid => $id,
 | 
|---|
| 641 |         fbparent => $args{parent},
 | 
|---|
| 642 |         );
 | 
|---|
| 643 |     push @flist, \%row;
 | 
|---|
| 644 |   }
 | 
|---|
| 645 |   return \@flist;
 | 
|---|
| 646 | } # end listFree()
 | 
|---|
| 647 | 
 | 
|---|
| 648 | 
 | 
|---|
| 649 | ## IPDB::listPool()
 | 
|---|
| 650 | #
 | 
|---|
| 651 | sub listPool {
 | 
|---|
| 652 |   my $dbh = shift;
 | 
|---|
| 653 |   my $pool = shift;
 | 
|---|
| 654 | 
 | 
|---|
| 655 |   my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,id".
 | 
|---|
| 656 |         " FROM poolips WHERE parent_id = ? ORDER BY ip");
 | 
|---|
| 657 |   $sth->execute($pool);
 | 
|---|
| 658 |   my @poolips;
 | 
|---|
| 659 |   while (my ($ip,$custid,$available,$desc,$type,$id) = $sth->fetchrow_array) {
 | 
|---|
| 660 |     my %row = (
 | 
|---|
| 661 |         ip => $ip,
 | 
|---|
| 662 |         custid => $custid,
 | 
|---|
| 663 |         available => $available,
 | 
|---|
| 664 |         desc => $desc,
 | 
|---|
| 665 |         delme => $available eq 'n',
 | 
|---|
| 666 |         parent => $pool,
 | 
|---|
| 667 |         id => $id,
 | 
|---|
| 668 |         );
 | 
|---|
| 669 |     push @poolips, \%row;
 | 
|---|
| 670 |   }
 | 
|---|
| 671 |   return \@poolips;
 | 
|---|
| 672 | } # end listPool()
 | 
|---|
| 673 | 
 | 
|---|
| 674 | 
 | 
|---|
| 675 | ## IPDB::getMasterList()
 | 
|---|
| 676 | # Get a list of master blocks, optionally including last-modified timestamps
 | 
|---|
| 677 | # Takes an optional flag to indicate whether to include timestamps;
 | 
|---|
| 678 | #  'm' includes ctime, all others (suggest 'c') do not.
 | 
|---|
| 679 | # Returns an arrayref to a list of hashrefs
 | 
|---|
| 680 | sub getMasterList {
 | 
|---|
| 681 |   my $dbh = shift;
 | 
|---|
| 682 |   my $stampme = shift || 'm';   # optional but should be set by caller for clarity
 | 
|---|
| 683 | 
 | 
|---|
| 684 |   my $mlist = $dbh->selectall_arrayref("SELECT id,vrf,cidr AS master".($stampme eq 'm' ? ',modifystamp AS mtime' : '').
 | 
|---|
| 685 |         " FROM allocations WHERE type='mm' ORDER BY cidr", { Slice => {} });
 | 
|---|
| 686 |   return $mlist;
 | 
|---|
| 687 | } # end getMasterList()
 | 
|---|
| 688 | 
 | 
|---|
| 689 | 
 | 
|---|
| 690 | ## IPDB::getTypeList()
 | 
|---|
| 691 | # Get an alloctype/description pair list suitable for dropdowns
 | 
|---|
| 692 | # Takes a flag to determine which general groups of types are returned
 | 
|---|
| 693 | # Returns an reference to an array of hashrefs
 | 
|---|
| 694 | sub getTypeList {
 | 
|---|
| 695 |   my $dbh = shift;
 | 
|---|
| 696 |   my $tgroup = shift || 'a';    # technically optional, like this, but should
 | 
|---|
| 697 |                                 # really be specified in the call for clarity
 | 
|---|
| 698 |   my $tlist;
 | 
|---|
| 699 |   if ($tgroup eq 'n') {
 | 
|---|
| 700 |     # grouping 'p' - all netblock types.  These include routed blocks, containers (_c)
 | 
|---|
| 701 |     # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p),
 | 
|---|
| 702 |     # and the "miscellaneous" cn, in, and en types.
 | 
|---|
| 703 |     $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
 | 
|---|
| 704 |         "AND type NOT LIKE '_i' ORDER BY listorder", { Slice => {} });
 | 
|---|
| 705 |   } elsif ($tgroup eq 'p') {
 | 
|---|
| 706 |     # grouping 'p' - primary allocation types.  As with 'n' above but without the _r contained types.
 | 
|---|
| 707 |     $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
 | 
|---|
| 708 |         "AND type NOT LIKE '_i' AND type NOT LIKE '_r' ORDER BY listorder", { Slice => {} });
 | 
|---|
| 709 |   } elsif ($tgroup eq 'c') {
 | 
|---|
| 710 |     # grouping 'c' - contained types.  These include all static IPs and all _r types.
 | 
|---|
| 711 |     $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
 | 
|---|
| 712 |         " AND (type LIKE '_i' OR type LIKE '_r') ORDER BY listorder", { Slice => {} });
 | 
|---|
| 713 |   } elsif ($tgroup eq 'i') {
 | 
|---|
| 714 |     # grouping 'i' - static IP types.
 | 
|---|
| 715 |     $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
 | 
|---|
| 716 |         " AND type LIKE '_i' ORDER BY listorder", { Slice => {} });
 | 
|---|
| 717 |   } else {
 | 
|---|
| 718 |     # grouping 'a' - all standard allocation types.  This includes everything
 | 
|---|
| 719 |     # but mm (present only as a formality).  Make this the default.
 | 
|---|
| 720 |     $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ".
 | 
|---|
| 721 |         " ORDER BY listorder", { Slice => {} });
 | 
|---|
| 722 |   }
 | 
|---|
| 723 |   return $tlist;
 | 
|---|
| 724 | }
 | 
|---|
| 725 | 
 | 
|---|
| 726 | 
 | 
|---|
| 727 | ## IPDB::getPoolSelect()
 | 
|---|
| 728 | # Get a list of pools matching the passed city and type that have 1 or more free IPs
 | 
|---|
| 729 | # Returns an arrayref to a list of hashrefs
 | 
|---|
| 730 | sub getPoolSelect {
 | 
|---|
| 731 |   my $dbh = shift;
 | 
|---|
| 732 |   my $iptype = shift;
 | 
|---|
| 733 |   my $pcity = shift;
 | 
|---|
| 734 | 
 | 
|---|
| 735 |   my ($ptype) = ($iptype =~ /^(.)i$/);
 | 
|---|
| 736 |   return if !$ptype;
 | 
|---|
| 737 |   $ptype .= '_';
 | 
|---|
| 738 | 
 | 
|---|
| 739 |   my $plist = $dbh->selectall_arrayref(
 | 
|---|
| 740 |         "SELECT count(*) AS poolfree,p.pool AS poolblock, a.city AS poolcit, a.rdepth AS poolrdepth ".
 | 
|---|
| 741 |         "FROM poolips p ".
 | 
|---|
| 742 |         "JOIN allocations a ON p.pool=a.cidr ".
 | 
|---|
| 743 |         "WHERE p.available='y' AND a.city = ? AND p.type LIKE ? ".
 | 
|---|
| 744 |         "GROUP BY p.pool,a.city,a.rdepth",
 | 
|---|
| 745 |         { Slice => {} }, ($pcity, $ptype) );
 | 
|---|
| 746 |   return $plist;
 | 
|---|
| 747 | } # end getPoolSelect()
 | 
|---|
| 748 | 
 | 
|---|
| 749 | 
 | 
|---|
| 750 | ## IPDB::findAllocateFrom()
 | 
|---|
| 751 | # Find free block to add a new allocation from.  (CIDR block version of pool select above, more or less)
 | 
|---|
| 752 | # Takes
 | 
|---|
| 753 | #  - mask length
 | 
|---|
| 754 | #  - allocation type
 | 
|---|
| 755 | #  - POP city "parent"
 | 
|---|
| 756 | #  - optional master-block restriction
 | 
|---|
| 757 | #  - optional flag to allow automatic pick-from-private-network-ranges
 | 
|---|
| 758 | # Returns a string with the first CIDR block matching the criteria, if any
 | 
|---|
| 759 | sub findAllocateFrom {
 | 
|---|
| 760 |   my $dbh = shift;
 | 
|---|
| 761 |   my $maskbits = shift;
 | 
|---|
| 762 |   my $type = shift;
 | 
|---|
| 763 |   my $city = shift;
 | 
|---|
| 764 |   my $pop = shift;
 | 
|---|
| 765 |   my %optargs = @_;
 | 
|---|
| 766 | 
 | 
|---|
| 767 |   my $failmsg = "No suitable free block found\n";
 | 
|---|
| 768 | 
 | 
|---|
| 769 |   my @vallist;
 | 
|---|
| 770 |   my $sql;
 | 
|---|
| 771 | 
 | 
|---|
| 772 |   # Free pool IPs should be easy.
 | 
|---|
| 773 |   if ($type =~ /^.i$/) {
 | 
|---|
| 774 |     # User may get an IP from the wrong VRF.  User should not be using admin tools to allocate static IPs.
 | 
|---|
| 775 |     $sql = "SELECT id, ip, parent_id FROM poolips WHERE ip = ?";
 | 
|---|
| 776 |     @vallist = ($optargs{gimme});
 | 
|---|
| 777 |   } else {
 | 
|---|
| 778 | 
 | 
|---|
| 779 | ## Set up the SQL to find out what freeblock we can (probably) use for an allocation.
 | 
|---|
| 780 | ## Very large systems will require development of a reserve system (possibly an extension
 | 
|---|
| 781 | ## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?)
 | 
|---|
| 782 | ## Also populate a value list for the DBI call.
 | 
|---|
| 783 | 
 | 
|---|
| 784 |     @vallist = ($maskbits);
 | 
|---|
| 785 |     $sql = "SELECT id,cidr,parent_id FROM freeblocks WHERE masklen(cidr) <= ?";
 | 
|---|
| 786 | 
 | 
|---|
| 787 | # cases, strict rules
 | 
|---|
| 788 | # .c  ->  container type
 | 
|---|
| 789 | #   requires a routing container, fbtype r
 | 
|---|
| 790 | # .d  ->  DHCP/"normal-routing" static pool
 | 
|---|
| 791 | #   requires a routing container, fbtype r
 | 
|---|
| 792 | # .e  ->  Dynamic-assignment connectivity
 | 
|---|
| 793 | #   requires a routing container, fbtype r
 | 
|---|
| 794 | # .i  ->  error, can't allocate static IPs this way?
 | 
|---|
| 795 | # mm  ->  error, master block
 | 
|---|
| 796 | # rm  ->  routed block
 | 
|---|
| 797 | #   requires master block, fbtype m
 | 
|---|
| 798 | # .n  ->  Miscellaneous usage
 | 
|---|
| 799 | #   requires a routing container, fbtype r
 | 
|---|
| 800 | # .p  ->  PPP(oE) static pool
 | 
|---|
| 801 | #   requires a routing container, fbtype r
 | 
|---|
| 802 | # .r  ->  contained type
 | 
|---|
| 803 | #   requires a matching container, fbtype $1
 | 
|---|
| 804 | ##fixme:  strict-or-not flag
 | 
|---|
| 805 | 
 | 
|---|
| 806 | ##fixme:  config or UI flag for "Strict" mode
 | 
|---|
| 807 | # if ($strictmode) {
 | 
|---|
| 808 | if (0) {
 | 
|---|
| 809 |   if ($type =~ /^(.)r$/) {
 | 
|---|
| 810 |     push @vallist, $1;
 | 
|---|
| 811 |     $sql .= " AND routed = ?";
 | 
|---|
| 812 |   } elsif ($type eq 'rm') {
 | 
|---|
| 813 |     $sql .= " AND routed = 'm'";
 | 
|---|
| 814 |   } else {
 | 
|---|
| 815 |     $sql .= " AND routed = 'r'";
 | 
|---|
| 816 |   }
 | 
|---|
| 817 | }
 | 
|---|
| 818 | 
 | 
|---|
| 819 |     # for PPP(oE) and container types, the POP city is the one attached to the pool.
 | 
|---|
| 820 |     # individual allocations get listed with the customer city site.
 | 
|---|
| 821 |     ##fixme:  chain cities to align roughly with a full layer-2 node graph
 | 
|---|
| 822 |     $city = $pop if $type !~ /^.[pc]$/;
 | 
|---|
| 823 |     if ($type ne 'rm' && $city) {
 | 
|---|
| 824 |       $sql .= " AND city = ?";
 | 
|---|
| 825 |       push @vallist, $city;
 | 
|---|
| 826 |     }
 | 
|---|
| 827 |     # Allow specifying an arbitrary full block, instead of a master
 | 
|---|
| 828 |     if ($optargs{gimme}) {
 | 
|---|
| 829 |       $sql .= " AND cidr >>= ?";
 | 
|---|
| 830 |       push @vallist, $optargs{gimme};
 | 
|---|
| 831 |     }
 | 
|---|
| 832 |     # if a specific master was requested, allow the requestor to self->shoot(foot)
 | 
|---|
| 833 |     if ($optargs{master} && $optargs{master} ne '-') {
 | 
|---|
| 834 |       $sql .= " AND master_id = ?";
 | 
|---|
| 835 | # if $optargs{master} ne '-';
 | 
|---|
| 836 |       push @vallist, $optargs{master};
 | 
|---|
| 837 |     } else {
 | 
|---|
| 838 |       # if a specific master was NOT requested, filter out the RFC 1918 private networks
 | 
|---|
| 839 |       if (!$optargs{allowpriv}) {
 | 
|---|
| 840 |         $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')";
 | 
|---|
| 841 |       }
 | 
|---|
| 842 |     }
 | 
|---|
| 843 |     # Sorting and limiting, since we don't (currently) care to provide a selection of
 | 
|---|
| 844 |     # blocks to carve up.  This preserves something resembling optimal usage of the IP
 | 
|---|
| 845 |     # space by forcing contiguous allocations and free blocks as much as possible.
 | 
|---|
| 846 |     $sql .= " ORDER BY masklen(cidr) DESC,cidr LIMIT 1";
 | 
|---|
| 847 |   } # done setting up SQL for free CIDR block
 | 
|---|
| 848 | 
 | 
|---|
| 849 |   my ($fbid,$fbfound,$fbparent) = $dbh->selectrow_array($sql, undef, @vallist);
 | 
|---|
| 850 |   return $fbid,$fbfound,$fbparent;
 | 
|---|
| 851 | } # end findAllocateFrom()
 | 
|---|
| 852 | 
 | 
|---|
| 853 | 
 | 
|---|
| 854 | ## IPDB::ipParent()
 | 
|---|
| 855 | # Get an IP's parent pool's details
 | 
|---|
| 856 | # Takes a database handle and IP
 | 
|---|
| 857 | # Returns a hashref to the parent pool block, if any
 | 
|---|
| 858 | sub ipParent {
 | 
|---|
| 859 |   my $dbh = shift;
 | 
|---|
| 860 |   my $block = shift;
 | 
|---|
| 861 | 
 | 
|---|
| 862 |   my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
 | 
|---|
| 863 |         " WHERE cidr >>= ? AND (type LIKE '_p' OR type LIKE '_d')", undef, ($block) );
 | 
|---|
| 864 |   return $pinfo;
 | 
|---|
| 865 | } # end ipParent()
 | 
|---|
| 866 | 
 | 
|---|
| 867 | 
 | 
|---|
| 868 | ## IPDB::subParent()
 | 
|---|
| 869 | # Get a block's parent's details
 | 
|---|
| 870 | # Takes a database handle and CIDR block
 | 
|---|
| 871 | # Returns a hashref to the parent container block, if any
 | 
|---|
| 872 | sub subParent {
 | 
|---|
| 873 |   my $dbh = shift;
 | 
|---|
| 874 |   my $block = shift;
 | 
|---|
| 875 | 
 | 
|---|
| 876 |   my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations".
 | 
|---|
| 877 |         " WHERE cidr >>= ?", undef, ($block) );
 | 
|---|
| 878 |   return $pinfo;
 | 
|---|
| 879 | } # end subParent()
 | 
|---|
| 880 | 
 | 
|---|
| 881 | 
 | 
|---|
| 882 | ## IPDB::blockParent()
 | 
|---|
| 883 | # Get a block's parent's details
 | 
|---|
| 884 | # Takes a database handle and CIDR block
 | 
|---|
| 885 | # Returns a hashref to the parent container block, if any
 | 
|---|
| 886 | sub blockParent {
 | 
|---|
| 887 |   my $dbh = shift;
 | 
|---|
| 888 |   my $block = shift;
 | 
|---|
| 889 | 
 | 
|---|
| 890 |   my $pinfo = $dbh->selectrow_hashref("SELECT cidr,city FROM routed".
 | 
|---|
| 891 |         " WHERE cidr >>= ?", undef, ($block) );
 | 
|---|
| 892 |   return $pinfo;
 | 
|---|
| 893 | } # end blockParent()
 | 
|---|
| 894 | 
 | 
|---|
| 895 | 
 | 
|---|
| 896 | ## IPDB::getRoutedCity()
 | 
|---|
| 897 | # Get the city for a routed block.
 | 
|---|
| 898 | sub getRoutedCity {
 | 
|---|
| 899 |   my $dbh = shift;
 | 
|---|
| 900 |   my $block = shift;
 | 
|---|
| 901 | 
 | 
|---|
| 902 |   my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) );
 | 
|---|
| 903 |   return $rcity;
 | 
|---|
| 904 | } # end getRoutedCity()
 | 
|---|
| 905 | 
 | 
|---|
| 906 | 
 | 
|---|
| 907 | ## IPDB::allocateBlock()
 | 
|---|
| 908 | # Does all of the magic of actually allocating a netblock
 | 
|---|
| 909 | # Requires a database handle, and a hash containing the block to allocate, routing depth, custid,
 | 
|---|
| 910 | #   type, city, block to allocate from, and optionally a description, notes, circuit ID,
 | 
|---|
| 911 | #   and private data
 | 
|---|
| 912 | # Returns a success code and optional error message.
 | 
|---|
| 913 | sub allocateBlock {
 | 
|---|
| 914 |   my $dbh = shift;
 | 
|---|
| 915 | 
 | 
|---|
| 916 |   my %args = @_;
 | 
|---|
| 917 | 
 | 
|---|
| 918 |   $args{cidr} = new NetAddr::IP $args{cidr};
 | 
|---|
| 919 | 
 | 
|---|
| 920 |   $args{desc} = '' if !$args{desc};
 | 
|---|
| 921 |   $args{notes} = '' if !$args{notes};
 | 
|---|
| 922 |   $args{circid} = '' if !$args{circid};
 | 
|---|
| 923 |   $args{privdata} = '' if !$args{privdata};
 | 
|---|
| 924 |   $args{vrf} = '' if !$args{vrf};
 | 
|---|
| 925 |   $args{rdns} = '' if !$args{rdns};
 | 
|---|
| 926 | 
 | 
|---|
| 927 |   my $sth;
 | 
|---|
| 928 | 
 | 
|---|
| 929 |   # Snag the "type" of the freeblock and its CIDR
 | 
|---|
| 930 |   my ($alloc_from_type, $alloc_from, $fbparent, $fcity, $fbmaster) =
 | 
|---|
| 931 |         $dbh->selectrow_array("SELECT routed,cidr,parent_id,city,master_id FROM freeblocks WHERE id = ?",
 | 
|---|
| 932 |         undef, $args{fbid});
 | 
|---|
| 933 |   $alloc_from = new NetAddr::IP $alloc_from;
 | 
|---|
| 934 | 
 | 
|---|
| 935 |   # To contain the error message, if any.
 | 
|---|
| 936 |   my $msg = "Unknown error allocating $args{cidr} as '$disp_alloctypes{$args{type}}'";
 | 
|---|
| 937 | 
 | 
|---|
| 938 |   # Enable transactions and error handling
 | 
|---|
| 939 |   local $dbh->{AutoCommit} = 0; # These need to be local so we don't
 | 
|---|
| 940 |   local $dbh->{RaiseError} = 1; # step on our toes by accident.
 | 
|---|
| 941 | 
 | 
|---|
| 942 |   if ($args{type} =~ /^.i$/) {
 | 
|---|
| 943 |     $msg = "Unable to assign static IP $args{cidr} to $args{custid}";
 | 
|---|
| 944 |     eval {
 | 
|---|
| 945 |       if ($args{cidr}) {        # IP specified
 | 
|---|
| 946 |         my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($args{cidr}) );
 | 
|---|
| 947 |         die "IP is not in an IP pool.\n"
 | 
|---|
| 948 |           if !$isavail;
 | 
|---|
| 949 |         die "IP already allocated.  Deallocate and reallocate, or update the entry\n"
 | 
|---|
| 950 |           if $isavail eq 'n';
 | 
|---|
| 951 |       } else {  # IP not specified, take first available
 | 
|---|
| 952 |         ($args{cidr}) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip",
 | 
|---|
| 953 |                 undef, ($args{alloc_from}) );
 | 
|---|
| 954 |       }
 | 
|---|
| 955 |       $dbh->do("UPDATE poolips SET custid = ?, city = ?,available='n', description = ?, notes = ?, ".
 | 
|---|
| 956 |         "circuitid = ?, privdata = ?, vrf = ?, rdns = ? ".
 | 
|---|
| 957 |         "WHERE ip = ? AND parent_id = ?", undef,
 | 
|---|
| 958 |                 ($args{custid}, $args{city}, $args{desc}, $args{notes},
 | 
|---|
| 959 |                 $args{circid}, $args{privdata}, $args{vrf}, $args{rdns},
 | 
|---|
| 960 |                 $args{cidr}, $args{parent}) );
 | 
|---|
| 961 | 
 | 
|---|
| 962 | # node hack
 | 
|---|
| 963 |       if ($args{nodeid} && $args{nodeid} ne '') {
 | 
|---|
| 964 |         $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
 | 
|---|
| 965 |       }
 | 
|---|
| 966 | # end node hack
 | 
|---|
| 967 | 
 | 
|---|
| 968 |       $dbh->commit;
 | 
|---|
| 969 |     };
 | 
|---|
| 970 |     if ($@) {
 | 
|---|
| 971 |       $msg .= ": $@";
 | 
|---|
| 972 |       eval { $dbh->rollback; };
 | 
|---|
| 973 |       return ('FAIL', $msg);
 | 
|---|
| 974 |     } else {
 | 
|---|
| 975 |       _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user});
 | 
|---|
| 976 |       return ('OK', $args{cidr});
 | 
|---|
| 977 |     }
 | 
|---|
| 978 | 
 | 
|---|
| 979 |   } else { # end IP-from-pool allocation
 | 
|---|
| 980 | 
 | 
|---|
| 981 |     if ($args{cidr} == $alloc_from) {
 | 
|---|
| 982 |       # Easiest case- insert in one table, delete in the other, and go home.  More or less.
 | 
|---|
| 983 |       # insert into allocations values (cidr,custid,type,city,desc) and
 | 
|---|
| 984 |       # delete from freeblocks where cidr='cidr'
 | 
|---|
| 985 |       # For data safety on non-transaction DBs, we delete first.
 | 
|---|
| 986 | 
 | 
|---|
| 987 |       eval {
 | 
|---|
| 988 |         $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
 | 
|---|
| 989 | 
 | 
|---|
| 990 |         # Insert the allocations entry
 | 
|---|
| 991 |         $dbh->do("INSERT INTO allocations ".
 | 
|---|
| 992 |                 "(cidr,parent_id,master_id,vrf,custid,type,city,description,notes,circuitid,privdata,rdns)".
 | 
|---|
| 993 |                 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", undef,
 | 
|---|
| 994 |                 ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{custid}, $args{type}, $args{city},
 | 
|---|
| 995 |                 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
 | 
|---|
| 996 |         my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 | 
|---|
| 997 | 
 | 
|---|
| 998 |         # Munge freeblocks
 | 
|---|
| 999 |         if ($args{type} =~ /^(.)[mc]$/) {
 | 
|---|
| 1000 |           # special case - block is a routed or container/"reserve" block
 | 
|---|
| 1001 |           my $rtype = $1;
 | 
|---|
| 1002 |           $dbh->do("UPDATE freeblocks SET routed = ?,city = ?,parent_id = ? WHERE id = ?",
 | 
|---|
| 1003 |                 undef, ($rtype, $args{city}, $bid, $args{fbid}) );
 | 
|---|
| 1004 |         } else {
 | 
|---|
| 1005 |           # "normal" case
 | 
|---|
| 1006 |           $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
 | 
|---|
| 1007 |         }
 | 
|---|
| 1008 | 
 | 
|---|
| 1009 |         # And initialize the pool, if necessary
 | 
|---|
| 1010 |         # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
 | 
|---|
| 1011 |         # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
 | 
|---|
| 1012 |         if ($args{type} =~ /^.p$/) {
 | 
|---|
| 1013 |           $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
 | 
|---|
| 1014 |           my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
 | 
|---|
| 1015 |           die $rmsg if $code eq 'FAIL';
 | 
|---|
| 1016 |         } elsif ($args{type} =~ /^.d$/) {
 | 
|---|
| 1017 |           $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
 | 
|---|
| 1018 |           my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
 | 
|---|
| 1019 |           die $rmsg if $code eq 'FAIL';
 | 
|---|
| 1020 |         }
 | 
|---|
| 1021 | 
 | 
|---|
| 1022 | # node hack
 | 
|---|
| 1023 |       if ($args{nodeid} && $args{nodeid} ne '') {
 | 
|---|
| 1024 |         $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
 | 
|---|
| 1025 |       }
 | 
|---|
| 1026 | # end node hack
 | 
|---|
| 1027 | 
 | 
|---|
| 1028 |         $dbh->commit;
 | 
|---|
| 1029 |       }; # end of eval
 | 
|---|
| 1030 |       if ($@) {
 | 
|---|
| 1031 |         $msg .= ": ".$@;
 | 
|---|
| 1032 |         eval { $dbh->rollback; };
 | 
|---|
| 1033 |         return ('FAIL',$msg);
 | 
|---|
| 1034 |       }
 | 
|---|
| 1035 | 
 | 
|---|
| 1036 |     } else { # cidr != alloc_from
 | 
|---|
| 1037 | 
 | 
|---|
| 1038 |       # Hard case.  Allocation is smaller than free block.
 | 
|---|
| 1039 | 
 | 
|---|
| 1040 |       # make sure new allocation is in fact within freeblock.  *sigh*
 | 
|---|
| 1041 |       return ('FAIL',"Requested allocation $args{cidr} is not within $alloc_from")
 | 
|---|
| 1042 |         if !$alloc_from->contains($args{cidr});
 | 
|---|
| 1043 |       my $wantmaskbits = $args{cidr}->masklen;
 | 
|---|
| 1044 |       my $maskbits = $alloc_from->masklen;
 | 
|---|
| 1045 | 
 | 
|---|
| 1046 |       my @newfreeblocks;        # Holds free blocks generated from splitting the source freeblock.
 | 
|---|
| 1047 | 
 | 
|---|
| 1048 |       # This determines which blocks will be left "free" after allocation.  We take the
 | 
|---|
| 1049 |       # block we're allocating from, and split it in half.  We see which half the wanted
 | 
|---|
| 1050 |       # block is in, and repeat until the wanted block is equal to one of the halves.
 | 
|---|
| 1051 |       my $i=0;
 | 
|---|
| 1052 |       my $tmp_from = $alloc_from;       # So we don't munge $args{alloc_from}
 | 
|---|
| 1053 |       while ($maskbits++ < $wantmaskbits) {
 | 
|---|
| 1054 |         my @subblocks = $tmp_from->split($maskbits);
 | 
|---|
| 1055 |         $newfreeblocks[$i++] = (($args{cidr}->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]);
 | 
|---|
| 1056 |         $tmp_from = ( ($args{cidr}->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] );
 | 
|---|
| 1057 |       } # while
 | 
|---|
| 1058 | 
 | 
|---|
| 1059 |       # Begin SQL transaction block
 | 
|---|
| 1060 |       eval {
 | 
|---|
| 1061 |         $msg = "Unable to allocate $args{cidr} as '$disp_alloctypes{$args{type}}'";
 | 
|---|
| 1062 | 
 | 
|---|
| 1063 |         # Delete old freeblocks entry
 | 
|---|
| 1064 |         $dbh->do("DELETE FROM freeblocks WHERE id = ?", undef, ($args{fbid}) );
 | 
|---|
| 1065 | 
 | 
|---|
| 1066 |         # Insert new list of smaller free blocks left over
 | 
|---|
| 1067 |         $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,vrf,parent_id,master_id) VALUES (?,?,?,?,?,?)");
 | 
|---|
| 1068 |         foreach my $block (@newfreeblocks) {
 | 
|---|
| 1069 |           $sth->execute($block, $fcity, $alloc_from_type, $args{vrf}, $fbparent, $fbmaster);
 | 
|---|
| 1070 |         }
 | 
|---|
| 1071 | 
 | 
|---|
| 1072 |         # Insert the allocations entry
 | 
|---|
| 1073 |         $dbh->do("INSERT INTO allocations ".
 | 
|---|
| 1074 |                 "(cidr,parent_id,master_id,vrf,custid,type,city,description,notes,circuitid,privdata,rdns)".
 | 
|---|
| 1075 |                 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", undef,
 | 
|---|
| 1076 |                 ($args{cidr}, $fbparent, $fbmaster, $args{vrf}, $args{custid}, $args{type}, $args{city},
 | 
|---|
| 1077 |                 $args{desc}, $args{notes}, $args{circid}, $args{privdata}, $args{rdns}) );
 | 
|---|
| 1078 |         my ($bid) = $dbh->selectrow_array("SELECT currval('allocations_id_seq')");
 | 
|---|
| 1079 | 
 | 
|---|
| 1080 |         # For routed/container types, add a freeblock within the allocated block so we can subdivide it further
 | 
|---|
| 1081 |         if ($args{type} =~ /(.)[mc]/) { # rm and .c types - containers
 | 
|---|
| 1082 |           my $rtype = $1;
 | 
|---|
| 1083 |           $sth->execute($args{cidr}, $args{city}, $rtype, $args{vrf}, $bid, $fbmaster);
 | 
|---|
| 1084 |         }
 | 
|---|
| 1085 | 
 | 
|---|
| 1086 |         # And initialize the pool, if necessary
 | 
|---|
| 1087 |         # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available
 | 
|---|
| 1088 |         # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed.
 | 
|---|
| 1089 |         if ($args{type} =~ /^.p$/) {
 | 
|---|
| 1090 |           $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
 | 
|---|
| 1091 |           my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "all", $bid);
 | 
|---|
| 1092 |           die $rmsg if $code eq 'FAIL';
 | 
|---|
| 1093 |         } elsif ($args{type} =~ /^.d$/) {
 | 
|---|
| 1094 |           $msg = "Could not initialize IPs in new $disp_alloctypes{$args{type}} $args{cidr}";
 | 
|---|
| 1095 |           my ($code,$rmsg) = initPool($dbh, $args{cidr}, $args{type}, $args{city}, "normal", $bid);
 | 
|---|
| 1096 |           die $rmsg if $code eq 'FAIL';
 | 
|---|
| 1097 |         }
 | 
|---|
| 1098 | 
 | 
|---|
| 1099 | # node hack
 | 
|---|
| 1100 |       if ($args{nodeid} && $args{nodeid} ne '') {
 | 
|---|
| 1101 |         $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{cidr}, $args{nodeid}) );
 | 
|---|
| 1102 |       }
 | 
|---|
| 1103 | # end node hack
 | 
|---|
| 1104 | 
 | 
|---|
| 1105 |         $dbh->commit;
 | 
|---|
| 1106 |       }; # end eval
 | 
|---|
| 1107 |       if ($@) {
 | 
|---|
| 1108 |         $msg .= ": ".$@;
 | 
|---|
| 1109 |         eval { $dbh->rollback; };
 | 
|---|
| 1110 |         return ('FAIL',$msg);
 | 
|---|
| 1111 |       }
 | 
|---|
| 1112 | 
 | 
|---|
| 1113 |     } # end fullcidr != alloc_from
 | 
|---|
| 1114 | 
 | 
|---|
| 1115 |     # now we do the DNS dance for netblocks, if we have an RPC server to do it with and a pattern to use.
 | 
|---|
| 1116 |     _rpc('addOrUpdateRevRec', cidr => "$args{cidr}", name => $args{rdns}, rpcuser => $args{user})
 | 
|---|
| 1117 |       if $args{rdns};
 | 
|---|
| 1118 | 
 | 
|---|
| 1119 |     return ('OK', 'OK');
 | 
|---|
| 1120 | 
 | 
|---|
| 1121 |   } # end static-IP vs netblock allocation
 | 
|---|
| 1122 | 
 | 
|---|
| 1123 | } # end allocateBlock()
 | 
|---|
| 1124 | 
 | 
|---|
| 1125 | 
 | 
|---|
| 1126 | ## IPDB::initPool()
 | 
|---|
| 1127 | # Initializes a pool
 | 
|---|
| 1128 | # Requires a database handle, the pool CIDR, type, city, and a parameter
 | 
|---|
| 1129 | # indicating whether the pool should allow allocation of literally every
 | 
|---|
| 1130 | # IP, or if it should reserve network/gateway/broadcast IPs
 | 
|---|
| 1131 | # Note that this is NOT done in a transaction, that's why it's a private
 | 
|---|
| 1132 | # function and should ONLY EVER get called from allocateBlock()
 | 
|---|
| 1133 | sub initPool {
 | 
|---|
| 1134 |   my ($dbh,undef,$type,$city,$class,$parent) = @_;
 | 
|---|
| 1135 |   my $pool = new NetAddr::IP $_[1];
 | 
|---|
| 1136 | 
 | 
|---|
| 1137 |   # IPv6 does not lend itself to IP pools as supported
 | 
|---|
| 1138 |   return ('FAIL',"Refusing to create IPv6 static IP pool") if $pool->{isv6};
 | 
|---|
| 1139 |   # IPv4 pools don't make much sense beyond even /24.  Allow up to 4096-host footshooting anyway.
 | 
|---|
| 1140 |   # NetAddr::IP won't allow more than a /16 (65k hosts).
 | 
|---|
| 1141 |   return ('FAIL',"Refusing to create oversized static IP pool") if $pool->masklen <= 20;
 | 
|---|
| 1142 | 
 | 
|---|
| 1143 |   my ($pcustid) = $dbh->selectrow_array("SELECT def_custid FROM alloctypes WHERE type=?", undef, ($type) );
 | 
|---|
| 1144 |   $type =~ s/[pd]$/i/;
 | 
|---|
| 1145 |   my $sth;
 | 
|---|
| 1146 |   my $msg;
 | 
|---|
| 1147 | 
 | 
|---|
| 1148 |   # Trap errors so we can pass them back to the caller.  Even if the
 | 
|---|
| 1149 |   # caller is only ever supposed to be local, and therefore already
 | 
|---|
| 1150 |   # trapping errors.  >:(
 | 
|---|
| 1151 |   local $dbh->{AutoCommit} = 0; # These need to be local so we don't
 | 
|---|
| 1152 |   local $dbh->{RaiseError} = 1; # step on our toes by accident.
 | 
|---|
| 1153 | 
 | 
|---|
| 1154 |   eval {
 | 
|---|
| 1155 |     # have to insert all pool IPs into poolips table as "unallocated".
 | 
|---|
| 1156 |     $sth = $dbh->prepare("INSERT INTO poolips (ip,custid,city,type,parent_id) VALUES (?,?,?,?,?)");
 | 
|---|
| 1157 |     my @poolip_list = $pool->hostenum;
 | 
|---|
| 1158 |     if ($class eq 'all') { # (DSL-ish block - *all* IPs available
 | 
|---|
| 1159 |       if ($pool->addr !~ /\.0$/) {      # .0 causes weirdness.
 | 
|---|
| 1160 |         $sth->execute($pool->addr, $pcustid, $city, $type, $parent);
 | 
|---|
| 1161 |       }
 | 
|---|
| 1162 |       for (my $i=0; $i<=$#poolip_list; $i++) {
 | 
|---|
| 1163 |         $sth->execute($poolip_list[$i]->addr, $pcustid, $city, $type, $parent);
 | 
|---|
| 1164 |       }
 | 
|---|
| 1165 |       $pool--;
 | 
|---|
| 1166 |       if ($pool->addr !~ /\.255$/) {    # .255 can cause weirdness.
 | 
|---|
| 1167 |         $sth->execute($pool->addr, $pcustid, $city, $type, $parent);
 | 
|---|
| 1168 |       }
 | 
|---|
| 1169 |     } else { # (real netblock)
 | 
|---|
| 1170 |       for (my $i=1; $i<=$#poolip_list; $i++) {
 | 
|---|
| 1171 |         $sth->execute($poolip_list[$i]->addr, $pcustid, $city, $type, $parent);
 | 
|---|
| 1172 |       }
 | 
|---|
| 1173 |     }
 | 
|---|
| 1174 | # don't commit here!  the caller may not be done.
 | 
|---|
| 1175 | #    $dbh->commit;
 | 
|---|
| 1176 |   };
 | 
|---|
| 1177 |   if ($@) {
 | 
|---|
| 1178 |     $msg = $@;
 | 
|---|
| 1179 | # Don't roll back!  It's up to the caller to handle this.
 | 
|---|
| 1180 | #    eval { $dbh->rollback; };
 | 
|---|
| 1181 |     return ('FAIL',$msg);
 | 
|---|
| 1182 |   } else {
 | 
|---|
| 1183 |     return ('OK',"OK");
 | 
|---|
| 1184 |   }
 | 
|---|
| 1185 | } # end initPool()
 | 
|---|
| 1186 | 
 | 
|---|
| 1187 | 
 | 
|---|
| 1188 | ## IPDB::updateBlock()
 | 
|---|
| 1189 | # Update an allocation
 | 
|---|
| 1190 | # Takes all allocation fields in a hash
 | 
|---|
| 1191 | sub updateBlock {
 | 
|---|
| 1192 |   my $dbh = shift;
 | 
|---|
| 1193 |   my %args = @_;
 | 
|---|
| 1194 | 
 | 
|---|
| 1195 |   return ('FAIL', 'Missing block to update') if !$args{block};
 | 
|---|
| 1196 | 
 | 
|---|
| 1197 |   # Spaces don't show up well in lots of places.  Make sure they don't get into the DB.
 | 
|---|
| 1198 |   $args{custid} =~ s/^\s+//;
 | 
|---|
| 1199 |   $args{custid} =~ s/\s+$//;
 | 
|---|
| 1200 | 
 | 
|---|
| 1201 |   # do it all in a transaction
 | 
|---|
| 1202 |   local $dbh->{AutoCommit} = 0;
 | 
|---|
| 1203 |   local $dbh->{RaiseError} = 1;
 | 
|---|
| 1204 | 
 | 
|---|
| 1205 |   my @fieldlist;
 | 
|---|
| 1206 |   my @vallist;
 | 
|---|
| 1207 |   foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'rdns') {
 | 
|---|
| 1208 |     if ($args{$_}) {
 | 
|---|
| 1209 |       push @fieldlist, $_;
 | 
|---|
| 1210 |       push @vallist, $args{$_};
 | 
|---|
| 1211 |     }
 | 
|---|
| 1212 |   }
 | 
|---|
| 1213 | 
 | 
|---|
| 1214 |   my $binfo;
 | 
|---|
| 1215 |   my $updtable = 'allocations';
 | 
|---|
| 1216 |   my $keyfield = 'id';
 | 
|---|
| 1217 |   if ($args{type} =~ /^(.)i$/) {
 | 
|---|
| 1218 |     $updtable = 'poolips';
 | 
|---|
| 1219 |     $binfo = getBlockData($dbh, $args{block}, 'i');
 | 
|---|
| 1220 |   } else {
 | 
|---|
| 1221 | ## fixme:  there's got to be a better way...
 | 
|---|
| 1222 |     $binfo = getBlockData($dbh, $args{block});
 | 
|---|
| 1223 |     if ($args{swip}) {
 | 
|---|
| 1224 |       if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') {
 | 
|---|
| 1225 |         $args{swip} = 'y';
 | 
|---|
| 1226 |       } else {
 | 
|---|
| 1227 |         $args{swip} = 'n';
 | 
|---|
| 1228 |       }
 | 
|---|
| 1229 |     }
 | 
|---|
| 1230 |     foreach ('type', 'swip') {
 | 
|---|
| 1231 |       if ($args{$_}) {
 | 
|---|
| 1232 |         push @fieldlist, $_;
 | 
|---|
| 1233 |         push @vallist, $args{$_};
 | 
|---|
| 1234 |       }
 | 
|---|
| 1235 |     }
 | 
|---|
| 1236 |   }
 | 
|---|
| 1237 | 
 | 
|---|
| 1238 |   return ('FAIL', 'No fields to update') if !@fieldlist;
 | 
|---|
| 1239 | 
 | 
|---|
| 1240 |   push @vallist, $args{block};
 | 
|---|
| 1241 |   my $sql = "UPDATE $updtable SET ";
 | 
|---|
| 1242 |   $sql .= join " = ?, ", @fieldlist;
 | 
|---|
| 1243 |   $sql .= " = ? WHERE $keyfield = ?";
 | 
|---|
| 1244 | 
 | 
|---|
| 1245 |   eval {
 | 
|---|
| 1246 |     # do the update
 | 
|---|
| 1247 |     $dbh->do($sql, undef, @vallist);
 | 
|---|
| 1248 | 
 | 
|---|
| 1249 |     if ($args{node}) {
 | 
|---|
| 1250 |       # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there
 | 
|---|
| 1251 |       $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($binfo->{block}) );
 | 
|---|
| 1252 |       $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($binfo->{block}, $args{node}) )
 | 
|---|
| 1253 |         if $args{node} ne '--';
 | 
|---|
| 1254 |     }
 | 
|---|
| 1255 | 
 | 
|---|
| 1256 |     $dbh->commit;
 | 
|---|
| 1257 |   };
 | 
|---|
| 1258 |   if ($@) {
 | 
|---|
| 1259 |     my $msg = $@;
 | 
|---|
| 1260 |     $dbh->rollback;
 | 
|---|
| 1261 |     return ('FAIL', $msg);
 | 
|---|
| 1262 |   }
 | 
|---|
| 1263 | 
 | 
|---|
| 1264 |   $binfo->{block} =~ s|/32$||;
 | 
|---|
| 1265 |   _rpc('addOrUpdateRevRec', cidr => $binfo->{block}, name => $args{rdns}, rpcuser => $args{user});
 | 
|---|
| 1266 |   return ('OK','OK');
 | 
|---|
| 1267 | } # end updateBlock()
 | 
|---|
| 1268 | 
 | 
|---|
| 1269 | 
 | 
|---|
| 1270 | ## IPDB::deleteBlock()
 | 
|---|
| 1271 | # Removes an allocation from the database, including deleting IPs
 | 
|---|
| 1272 | # from poolips and recombining entries in freeblocks if possible
 | 
|---|
| 1273 | # Also handles "deleting" a static IP allocation, and removal of a master
 | 
|---|
| 1274 | # Requires a database handle, the block to delete, the routing depth (if applicable),
 | 
|---|
| 1275 | # the VRF ID, and a flag to indicate whether to delete associated forward DNS entries
 | 
|---|
| 1276 | # as well as the reverse entry
 | 
|---|
| 1277 | sub deleteBlock {
 | 
|---|
| 1278 |   my ($dbh,$id,$basetype,$delfwd,$user) = @_;
 | 
|---|
| 1279 | 
 | 
|---|
| 1280 |   # Collect info about the block we're going to delete
 | 
|---|
| 1281 |   my $binfo = getBlockData($dbh, $id, $basetype);
 | 
|---|
| 1282 |   my $cidr = new NetAddr::IP $binfo->{block};
 | 
|---|
| 1283 | 
 | 
|---|
| 1284 | # For possible auto-VRF-ignoring (since public IPs shouldn't usually be present in more than one VRF)
 | 
|---|
| 1285 | # is_rfc1918 requires NetAddr::IP >= 4.059
 | 
|---|
| 1286 | # rather than doing this over and over and over.....
 | 
|---|
| 1287 |   my $tmpnum = $cidr->numeric;
 | 
|---|
| 1288 | # 192.168.0.0/16 -> 192.168.255.255  =>  3232235520 -> 3232301055
 | 
|---|
| 1289 | # 172.16.0.0/12  -> 172.31.255.255   =>  2886729728 -> 2887778303
 | 
|---|
| 1290 | # 10.0.0.0/8     -> 10.255.255.255   =>  167772160  -> 184549375
 | 
|---|
| 1291 |   my $isprivnet = (3232235520 <= $tmpnum && $tmpnum <= 3232301055) ||
 | 
|---|
| 1292 |         (2886729728 <= $tmpnum && $tmpnum <= 2887778303) ||
 | 
|---|
| 1293 |         (167772160 <= $tmpnum && $tmpnum <= 184549375);
 | 
|---|
| 1294 | 
 | 
|---|
| 1295 |   my $sth;
 | 
|---|
| 1296 | 
 | 
|---|
| 1297 |   # Magic variables used for odd allocation cases.
 | 
|---|
| 1298 |   my $container;
 | 
|---|
| 1299 |   my $con_type;
 | 
|---|
| 1300 | 
 | 
|---|
| 1301 | 
 | 
|---|
| 1302 |   # temporarily forced null, until a sane UI for VRF tracking can be found.
 | 
|---|
| 1303 | #  $vrf = '';# if !$vrf;        # as with SQL, the null value is not equal to ''.  *sigh*
 | 
|---|
| 1304 | 
 | 
|---|
| 1305 |   # To contain the error message, if any.
 | 
|---|
| 1306 |   my $msg = "Unknown error deallocating $binfo->{type} $cidr";
 | 
|---|
| 1307 |   my $goback;   # to put the parent in so we can link back where the deallocate started
 | 
|---|
| 1308 | 
 | 
|---|
| 1309 |   # Enable transactions and exception-on-errors... but only for this sub
 | 
|---|
| 1310 |   local $dbh->{AutoCommit} = 0;
 | 
|---|
| 1311 |   local $dbh->{RaiseError} = 1;
 | 
|---|
| 1312 | 
 | 
|---|
| 1313 |   if ($binfo->{type} =~ /^.i$/) {
 | 
|---|
| 1314 |     # First case.  The "block" is a static IP
 | 
|---|
| 1315 |     # Note that we still need some additional code in the odd case
 | 
|---|
| 1316 |     # of a netblock-aligned contiguous group of static IPs
 | 
|---|
| 1317 | 
 | 
|---|
| 1318 |     eval {
 | 
|---|
| 1319 |       $msg = "Unable to deallocate $disp_alloctypes{$binfo->{type}} $cidr";
 | 
|---|
| 1320 |       my $pinfo = getBlockData($dbh, $binfo->{parent_id}, 'b');
 | 
|---|
| 1321 | ##fixme: VRF and rdepth
 | 
|---|
| 1322 |       $dbh->do("UPDATE poolips SET custid = ?, available = 'y',".
 | 
|---|
| 1323 |         "city = (SELECT city FROM allocations WHERE id = ?),".
 | 
|---|
| 1324 |         "description = '', notes = '', circuitid = '', vrf = ? WHERE id = ?", undef,
 | 
|---|
| 1325 |         ($pinfo->{custid}, $binfo->{parent_id}, $pinfo->{vrf}, $id) );
 | 
|---|
| 1326 |       $dbh->commit;
 | 
|---|
| 1327 |     };
 | 
|---|
| 1328 |     if ($@) {
 | 
|---|
| 1329 |       $msg .= ": $@";
 | 
|---|
| 1330 |       eval { $dbh->rollback; };
 | 
|---|
| 1331 |       return ('FAIL',$msg);
 | 
|---|
| 1332 |     } else {
 | 
|---|
| 1333 | ##fixme:  RPC return code?
 | 
|---|
| 1334 |       _rpc('delByCIDR', cidr => "$cidr", user => $user, delforward => $delfwd, rpcuser => $user);
 | 
|---|
| 1335 |       return ('OK',"OK");
 | 
|---|
| 1336 |     }
 | 
|---|
| 1337 | 
 | 
|---|
| 1338 |   } elsif ($binfo->{type} eq 'mm') { # end alloctype =~ /.i/
 | 
|---|
| 1339 |     # Second case.  The block is a full master block
 | 
|---|
| 1340 | 
 | 
|---|
| 1341 | ##fixme: VRF limit
 | 
|---|
| 1342 |     $msg = "Unable to delete master block $cidr";
 | 
|---|
| 1343 |     eval {
 | 
|---|
| 1344 |       $dbh->do("DELETE FROM allocations WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
 | 
|---|
| 1345 |       $dbh->do("DELETE FROM freeblocks WHERE cidr <<= ? AND master_id = ?", undef, ($cidr, $binfo->{master_id}) );
 | 
|---|
| 1346 |       $dbh->commit;
 | 
|---|
| 1347 |     };
 | 
|---|
| 1348 |     if ($@) {
 | 
|---|
| 1349 |       $msg .= ": $@";
 | 
|---|
| 1350 |       eval { $dbh->rollback; };
 | 
|---|
| 1351 |       return ('FAIL', $msg);
 | 
|---|
| 1352 |     }
 | 
|---|
| 1353 | 
 | 
|---|
| 1354 |     # Have to handle potentially split reverse zones.  Assume they *are* split,
 | 
|---|
| 1355 |     # since if we added them here, they would have been added split.
 | 
|---|
| 1356 | # allow splitting reverse zones to be disabled, maybe, someday
 | 
|---|
| 1357 | #if ($splitrevzones && !$cidr->{isv6}) {
 | 
|---|
| 1358 |     my @zonelist;
 | 
|---|
| 1359 |     if (1 && !$cidr->{isv6}) {
 | 
|---|
| 1360 |       my $splitpoint = ($cidr->masklen <= 16 ? 16 : 24);        # hack pthui
 | 
|---|
| 1361 |       @zonelist = $cidr->split($splitpoint);
 | 
|---|
| 1362 |     } else {
 | 
|---|
| 1363 |       @zonelist = ($cidr);
 | 
|---|
| 1364 |     }
 | 
|---|
| 1365 |     my @fails;
 | 
|---|
| 1366 |     foreach my $subzone (@zonelist) {
 | 
|---|
| 1367 |       if ($rpc_url && !_rpc('delZone', zone => "$subzone", revrec => 'y', rpcuser => $user, delforward => $delfwd) ) {
 | 
|---|
| 1368 |         push @fails, ("$subzone" => $errstr);
 | 
|---|
| 1369 |       }
 | 
|---|
| 1370 |     }
 | 
|---|
| 1371 |     if (@fails) {
 | 
|---|
| 1372 |       return ('WARN',"Warning(s) deleting $cidr from reverse DNS:\n".join("\n", @fails));
 | 
|---|
| 1373 |     }
 | 
|---|
| 1374 |     return ('OK','OK');
 | 
|---|
| 1375 | 
 | 
|---|
| 1376 |   } else { # end alloctype master block case
 | 
|---|
| 1377 | 
 | 
|---|
| 1378 |     ## This is a big block; but it HAS to be done in a chunk.  Any removal
 | 
|---|
| 1379 |     ## of a netblock allocation may result in a larger chunk of free
 | 
|---|
| 1380 |     ## contiguous IP space - which may in turn be combined into a single
 | 
|---|
| 1381 |     ## netblock rather than a number of smaller netblocks.
 | 
|---|
| 1382 | 
 | 
|---|
| 1383 |     my $retcode = 'OK';
 | 
|---|
| 1384 |     my ($ptype,$pcity,$ppatt,$p_id);
 | 
|---|
| 1385 | 
 | 
|---|
| 1386 |     eval {
 | 
|---|
| 1387 | 
 | 
|---|
| 1388 | ##fixme:  add recursive flag to allow "YES DAMMIT DELETE ALL EVARYTHING!!1!!" without
 | 
|---|
| 1389 | # explicitly deleting any suballocations of the block to be deleted.
 | 
|---|
| 1390 | 
 | 
|---|
| 1391 |       # get parent info of the block we're deleting
 | 
|---|
| 1392 |       my $pinfo = getBlockData($dbh, $binfo->{parent_id});
 | 
|---|
| 1393 |       $ptype = $pinfo->{type};
 | 
|---|
| 1394 |       $pcity = $pinfo->{city};
 | 
|---|
| 1395 |       $ppatt = $pinfo->{rdns};
 | 
|---|
| 1396 |       $p_id = $binfo->{parent_id};
 | 
|---|
| 1397 | 
 | 
|---|
| 1398 |       # Delete the block
 | 
|---|
| 1399 |       $dbh->do("DELETE FROM allocations WHERE id = ?", undef, ($id) );
 | 
|---|
| 1400 | 
 | 
|---|
| 1401 |       # munge the parent type a little
 | 
|---|
| 1402 |       $ptype = (split //, $ptype)[1];
 | 
|---|
| 1403 | 
 | 
|---|
| 1404 | ##fixme:  you can't...  CAN NOT....  assign the same public IP to multiple things.
 | 
|---|
| 1405 | #  'Net don't work like that, homey.  Restrict VRF-uniqueness to private IPs?
 | 
|---|
| 1406 | # -> $isprivnet flag from start of sub
 | 
|---|
| 1407 | 
 | 
|---|
| 1408 |       # check to see if any container allocations could be the "true" parent
 | 
|---|
| 1409 |       my ($tparent,$tpar_id,$trtype,$tcity);
 | 
|---|
| 1410 |       $tpar_id = 0;
 | 
|---|
| 1411 | 
 | 
|---|
| 1412 | ##fixme:  this is far simpler in the strict VRF case;  we "know" that any allocation
 | 
|---|
| 1413 | # contained by a container is a part of the same allocation tree when the VRF fields are equal.
 | 
|---|
| 1414 | 
 | 
|---|
| 1415 | # logic:
 | 
|---|
| 1416 | # For each possible container of $cidr
 | 
|---|
| 1417 | #  note the parent id
 | 
|---|
| 1418 | #  walk the chain up the parents
 | 
|---|
| 1419 | #    if we intersect $cidr's current parent, break
 | 
|---|
| 1420 | #  if we've intersected $cidr's current parent
 | 
|---|
| 1421 | #    set some variables to track that block
 | 
|---|
| 1422 | #    break
 | 
|---|
| 1423 | 
 | 
|---|
| 1424 | # Set up part of "is it in the middle of a pool?" check
 | 
|---|
| 1425 |       my $wuzpool = $dbh->selectrow_hashref("SELECT cidr,parent_id,type,city,custid,id FROM allocations ".
 | 
|---|
| 1426 |         "WHERE (type LIKE '_d' OR type LIKE '_p') AND cidr >> ? AND master_id = ?", { Slice => {} },
 | 
|---|
| 1427 |         ($cidr, $binfo->{master_id}) );
 | 
|---|
| 1428 | 
 | 
|---|
| 1429 | ##fixme?
 | 
|---|
| 1430 | # edge cases not handled, or handled badly:
 | 
|---|
| 1431 | # -> $cidr managed to get to be the entirety of an IP pool
 | 
|---|
| 1432 | 
 | 
|---|
| 1433 |       if ($wuzpool && $wuzpool->{id} != $id) {
 | 
|---|
| 1434 |         # we have legacy goo to be purified
 | 
|---|
| 1435 |         # going to ignore nested pools;  not possible to create them via API and no current legacy data includes any.
 | 
|---|
| 1436 | 
 | 
|---|
| 1437 |         # for convenience
 | 
|---|
| 1438 |         my $poolid = $wuzpool->{id};
 | 
|---|
| 1439 |         my $pool = $wuzpool->{cidr};
 | 
|---|
| 1440 |         my $poolcity = $wuzpool->{city};
 | 
|---|
| 1441 |         my $pooltype = $wuzpool->{type};
 | 
|---|
| 1442 |         my $poolcustid = $wuzpool->{custid};
 | 
|---|
| 1443 | 
 | 
|---|
| 1444 |         $retcode = 'WARNPOOL';
 | 
|---|
| 1445 |         $goback = "$poolid,$pool";
 | 
|---|
| 1446 |         # We've already deleted the block, now we have to stuff its IPs into the pool.
 | 
|---|
| 1447 |         $pooltype =~ s/[dp]$/i/;        # change type to static IP
 | 
|---|
| 1448 |         my $sth2 = $dbh->prepare("INSERT INTO poolips (ip,city,type,custid,parent_id) VALUES ".
 | 
|---|
| 1449 |           "(?,'$poolcity','$pooltype','$poolcustid',$poolid)");
 | 
|---|
| 1450 | 
 | 
|---|
| 1451 | ##fixme:  need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish)
 | 
|---|
| 1452 |         # don't insert .0
 | 
|---|
| 1453 |         $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|;
 | 
|---|
| 1454 |         $cidr++;
 | 
|---|
| 1455 |         my $bcast = $cidr->broadcast;
 | 
|---|
| 1456 |         while ($cidr != $bcast) {
 | 
|---|
| 1457 |           $sth2->execute($cidr->addr);
 | 
|---|
| 1458 |           $cidr++;
 | 
|---|
| 1459 |         }
 | 
|---|
| 1460 |         # don't insert .255
 | 
|---|
| 1461 |         $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|;
 | 
|---|
| 1462 | 
 | 
|---|
| 1463 | # Weirdness Happens.  $cidr goes read-only somewhere (this is a thing?!?),
 | 
|---|
| 1464 | # causing ->split, ->hostenum, and related methods to explode.  O_o
 | 
|---|
| 1465 | #        foreach my $ip ($cidr->hostenum) {
 | 
|---|
| 1466 | #          $sth2->execute($ip);
 | 
|---|
| 1467 | #        }
 | 
|---|
| 1468 | 
 | 
|---|
| 1469 |       }
 | 
|---|
| 1470 | 
 | 
|---|
| 1471 | ## important!
 | 
|---|
| 1472 | # ... or IS IT?
 | 
|---|
| 1473 | # we may have undef'ed $wuzpool above, if the allocation tree $cidr is in doesn't intersect the pool we found
 | 
|---|
| 1474 | #if (!$wuzpool) {
 | 
|---|
| 1475 | 
 | 
|---|
| 1476 |       else {
 | 
|---|
| 1477 | 
 | 
|---|
| 1478 | # Edge case:  Block is the same size as more than one parent level.  Should be rare.
 | 
|---|
| 1479 | # - mainly master + first routing.  Sorting on parent_id hides the problem pretty well,
 | 
|---|
| 1480 | # but it's likely still possible to fail in particularly well-mangled databases.
 | 
|---|
| 1481 | # The ultimate fix for this may be to resurrect the "routing depth" atrocity.  :/
 | 
|---|
| 1482 |         # Get all possible (and probably a number of impossible) containers for $cidr
 | 
|---|
| 1483 |         $sth = $dbh->prepare("SELECT cidr,parent_id,type,city,id FROM allocations ".
 | 
|---|
| 1484 |           "WHERE (type LIKE '_m' OR type LIKE '_c') AND cidr >>= ? AND master_id = ? ".
 | 
|---|
| 1485 |           "ORDER BY masklen(cidr) DESC,parent_id DESC");
 | 
|---|
| 1486 |         $sth->execute($cidr, $binfo->{master_id});
 | 
|---|
| 1487 | 
 | 
|---|
| 1488 |         # Quickly get certain fields (simpler than getBlockData()
 | 
|---|
| 1489 |         my $sth2 = $dbh->prepare("SELECT cidr,parent_id,type,city FROM allocations ".
 | 
|---|
| 1490 |           "WHERE (type LIKE '_m' OR type LIKE '_c') AND id = ? AND master_id = ?");
 | 
|---|
| 1491 | 
 | 
|---|
| 1492 |         # For each possible container of $cidr...
 | 
|---|
| 1493 |         while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 1494 |           my $i = 0;
 | 
|---|
| 1495 |           # Save some state and set a start point - parent ID of container we're checking
 | 
|---|
| 1496 |           $tparent = $data[0];
 | 
|---|
| 1497 |           my $ppid = $data[1];
 | 
|---|
| 1498 |           $trtype = $data[2];
 | 
|---|
| 1499 |           $tcity = $data[3];
 | 
|---|
| 1500 |           $tpar_id = $data[4];
 | 
|---|
| 1501 |           last if $data[4] == $binfo->{parent_id};  # Preemptively break if we're already in the right place
 | 
|---|
| 1502 |           last if $ppid == $binfo->{parent_id};     # ... or if the parent of the container is the block's parent
 | 
|---|
| 1503 |           while (1) {
 | 
|---|
| 1504 |             # Retrieve bits on that parent ID
 | 
|---|
| 1505 |             $sth2->execute($ppid, $binfo->{master_id});
 | 
|---|
| 1506 |             my @container = $sth2->fetchrow_array;
 | 
|---|
| 1507 |             $ppid = $container[1];
 | 
|---|
| 1508 |             last if $container[1] == 0;   # Break if we've hit a master block
 | 
|---|
| 1509 |             last if $ppid == $binfo->{parent_id};   # Break if we've reached the block $cidr is currently in
 | 
|---|
| 1510 |           }
 | 
|---|
| 1511 |           last if $ppid == $binfo->{parent_id};
 | 
|---|
| 1512 |         }
 | 
|---|
| 1513 | 
 | 
|---|
| 1514 |         # found an alternate parent;  reset some parent-info bits
 | 
|---|
| 1515 |         if ($tpar_id != $binfo->{parent_id}) {
 | 
|---|
| 1516 |           $ptype = (split //, $trtype)[1];
 | 
|---|
| 1517 |           $pcity = $tcity;
 | 
|---|
| 1518 |           $retcode = 'WARNMERGE';       # may be redundant
 | 
|---|
| 1519 |           $p_id = $tpar_id;
 | 
|---|
| 1520 |         }
 | 
|---|
| 1521 | 
 | 
|---|
| 1522 |         $goback = "$p_id,$tparent";     # breadcrumb, currently only used in case of live-parent-is-not-true-parent
 | 
|---|
| 1523 | 
 | 
|---|
| 1524 |         # Special case - delete pool IPs
 | 
|---|
| 1525 |         if ($binfo->{type} =~ /^.[pd]$/) {
 | 
|---|
| 1526 |           # We have to delete the IPs from the pool listing.
 | 
|---|
| 1527 | ##fixme:  rdepth?  vrf?
 | 
|---|
| 1528 |           $dbh->do("DELETE FROM poolips WHERE parent_id = ?", undef, ($id) );
 | 
|---|
| 1529 |         }
 | 
|---|
| 1530 | 
 | 
|---|
| 1531 |         $pinfo = getBlockData($dbh, $p_id);
 | 
|---|
| 1532 | 
 | 
|---|
| 1533 |         # If the block wasn't legacy goo embedded in a static pool, we check the
 | 
|---|
| 1534 |         # freeblocks in the identified parent to see if we can combine any of them.
 | 
|---|
| 1535 | 
 | 
|---|
| 1536 |         # if the block to be deleted is a container, move its freeblock(s) up a level, and reset their parenting info
 | 
|---|
| 1537 |         if ($binfo->{type} =~ /^.[mc]/) {
 | 
|---|
| 1538 |           # move the freeblocks into the parent
 | 
|---|
| 1539 |           # we don't insert a new freeblock because there could be a live reparented sub.
 | 
|---|
| 1540 |           $dbh->do("UPDATE freeblocks SET parent_id = ?, routed = ?, city = ? WHERE parent_id = ?", undef,
 | 
|---|
| 1541 |                 ($p_id, $ptype, $pcity, $id) );
 | 
|---|
| 1542 |         } else {
 | 
|---|
| 1543 |           # ... otherwise, add the freeblock
 | 
|---|
| 1544 |           $dbh->do("INSERT INTO freeblocks (cidr, city, routed, parent_id, master_id) VALUES (?,?,?,?,?)", undef,
 | 
|---|
| 1545 |                 ($cidr, $pcity, $ptype, $p_id, $binfo->{master_id}) );
 | 
|---|
| 1546 |         }
 | 
|---|
| 1547 | 
 | 
|---|
| 1548 | ##fixme: vrf
 | 
|---|
| 1549 | ##fixme:  simplify since all containers now represent different "layers"/"levels"?
 | 
|---|
| 1550 |         # set up the query to get the list of blocks to try to merge.
 | 
|---|
| 1551 |         $sth = $dbh->prepare("SELECT cidr,id FROM freeblocks ".
 | 
|---|
| 1552 |                 "WHERE parent_id = ? ".
 | 
|---|
| 1553 |                 "ORDER BY masklen(cidr) DESC");
 | 
|---|
| 1554 | 
 | 
|---|
| 1555 |         $sth->execute($p_id);
 | 
|---|
| 1556 | 
 | 
|---|
| 1557 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
 | 
|---|
| 1558 | # from the caller and the passed terms.
 | 
|---|
| 1559 | # EG:  if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
 | 
|---|
| 1560 | #       and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
 | 
|---|
| 1561 | #       .64-.95, and .96-.128), you will get an array containing a single
 | 
|---|
| 1562 | #       /25 as element 0 (.0-.127).  Order is not important;  you could have
 | 
|---|
| 1563 | #       $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
 | 
|---|
| 1564 | 
 | 
|---|
| 1565 |         my (@rawfb, @combinelist, %rawid);
 | 
|---|
| 1566 |         my $i=0;
 | 
|---|
| 1567 |         # for each free block under $parent, push a NetAddr::IP object into one list, and
 | 
|---|
| 1568 |         # continuously use NetAddr::IP->compact to automagically merge netblocks as possible.
 | 
|---|
| 1569 |         while (my @data = $sth->fetchrow_array) {
 | 
|---|
| 1570 |           my $testIP = new NetAddr::IP $data[0];
 | 
|---|
| 1571 |           push @rawfb, $testIP;
 | 
|---|
| 1572 |           $rawid{$data[0]} = $data[1];
 | 
|---|
| 1573 |           @combinelist = $testIP->compact(@combinelist);
 | 
|---|
| 1574 |         }
 | 
|---|
| 1575 | 
 | 
|---|
| 1576 |         # now that we have the full list of "compacted" freeblocks, go back over
 | 
|---|
| 1577 |         # the list of raw freeblocks, and delete the ones that got merged.
 | 
|---|
| 1578 |         $sth = $dbh->prepare("DELETE FROM freeblocks WHERE id = ?");
 | 
|---|
| 1579 |         foreach my $rawfree (@rawfb) {
 | 
|---|
| 1580 |           next if grep { $rawfree == $_ } @combinelist; # skip if the raw block is in the compacted list
 | 
|---|
| 1581 |           $sth->execute($rawid{$rawfree});
 | 
|---|
| 1582 |         }
 | 
|---|
| 1583 | 
 | 
|---|
| 1584 |         # now we walk the new list of compacted blocks, and see which ones we need to insert
 | 
|---|
| 1585 |         $sth = $dbh->prepare("INSERT INTO freeblocks (cidr,city,routed,parent_id,master_id) VALUES (?,?,?,?,?)");
 | 
|---|
| 1586 |         foreach my $cme (@combinelist) {
 | 
|---|
| 1587 |           next if grep { $cme == $_ } @rawfb;   # skip if the combined block was in the raw list
 | 
|---|
| 1588 |           $sth->execute($cme, $pcity, $ptype, $p_id, $binfo->{master_id});
 | 
|---|
| 1589 |         }
 | 
|---|
| 1590 | 
 | 
|---|
| 1591 |       } # done returning IPs to the appropriate place
 | 
|---|
| 1592 | 
 | 
|---|
| 1593 |       # If we got here, we've succeeded.  Whew!
 | 
|---|
| 1594 |       $dbh->commit;
 | 
|---|
| 1595 |     }; # end eval
 | 
|---|
| 1596 |     if ($@) {
 | 
|---|
| 1597 |       $msg .= ": $@";
 | 
|---|
| 1598 |       eval { $dbh->rollback; };
 | 
|---|
| 1599 |       return ('FAIL', $msg);
 | 
|---|
| 1600 |     } else {
 | 
|---|
| 1601 | ##fixme:  RPC return code?
 | 
|---|
| 1602 |       _rpc('delByCIDR', cidr => "$cidr", rpcuser => $user, delforward => $delfwd, delsubs => 'y', parpatt => $ppatt);
 | 
|---|
| 1603 |       return ($retcode, $goback);
 | 
|---|
| 1604 |     }
 | 
|---|
| 1605 | 
 | 
|---|
| 1606 |   } # end alloctype != netblock
 | 
|---|
| 1607 | 
 | 
|---|
| 1608 | } # end deleteBlock()
 | 
|---|
| 1609 | 
 | 
|---|
| 1610 | 
 | 
|---|
| 1611 | ## IPDB::getBlockData()
 | 
|---|
| 1612 | # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time,
 | 
|---|
| 1613 | # private/restricted data, for a CIDR block or pool IP
 | 
|---|
| 1614 | # Also returns SWIP status flag for CIDR blocks or pool netblock for IPs
 | 
|---|
| 1615 | # Takes the block ID or IP to look up and an optional flag to indicate a pool IP lookup
 | 
|---|
| 1616 | # instead of a netblock.
 | 
|---|
| 1617 | # Returns a hashref to the block data
 | 
|---|
| 1618 | sub getBlockData {
 | 
|---|
| 1619 |   my $dbh = shift;
 | 
|---|
| 1620 |   my $id = shift;
 | 
|---|
| 1621 |   my $type = shift || 'b';      # default to netblock for lazy callers
 | 
|---|
| 1622 | 
 | 
|---|
| 1623 |   # netblocks are in the allocations table;  pool IPs are in the poolips table.
 | 
|---|
| 1624 |   # If we try to look up a CIDR in an integer field we should just get back nothing.
 | 
|---|
| 1625 |   my ($btype) = $dbh->selectrow_array("SELECT type FROM allocations WHERE id=?", undef, ($id) );
 | 
|---|
| 1626 | 
 | 
|---|
| 1627 |   if ($type eq 'i') {
 | 
|---|
| 1628 |     my $binfo = $dbh->selectrow_hashref("SELECT ip AS block, custid, type, city, circuitid, description,".
 | 
|---|
| 1629 |         " notes, modifystamp AS lastmod, privdata, vrf, rdns, parent_id, master_id".
 | 
|---|
| 1630 |         " FROM poolips WHERE id = ?", undef, ($id) );
 | 
|---|
| 1631 |     return $binfo;
 | 
|---|
| 1632 |   } else {
 | 
|---|
| 1633 |     my $binfo = $dbh->selectrow_hashref("SELECT cidr AS block, custid, type, city, circuitid, ".
 | 
|---|
| 1634 |         "description, notes, modifystamp AS lastmod, privdata, vrf, swip, rdns, parent_id, master_id".
 | 
|---|
| 1635 |         " FROM allocations WHERE id = ?", undef, ($id) );
 | 
|---|
| 1636 |     return $binfo;
 | 
|---|
| 1637 |   }
 | 
|---|
| 1638 | } # end getBlockData()
 | 
|---|
| 1639 | 
 | 
|---|
| 1640 | 
 | 
|---|
| 1641 | ## IPDB::getBlockRDNS()
 | 
|---|
| 1642 | # Gets reverse DNS pattern for a block or IP.  Note that this will also
 | 
|---|
| 1643 | # retrieve any default pattern following the parent chain up, and check via
 | 
|---|
| 1644 | # RPC (if available) to see what the narrowest pattern for the requested block is
 | 
|---|
| 1645 | # Returns the current pattern for the block or IP.
 | 
|---|
| 1646 | sub getBlockRDNS {
 | 
|---|
| 1647 |   my $dbh = shift;
 | 
|---|
| 1648 |   my %args = @_;
 | 
|---|
| 1649 | 
 | 
|---|
| 1650 |   $args{type} = 'b' if !$args{type};
 | 
|---|
| 1651 | 
 | 
|---|
| 1652 |   # snag entry from database
 | 
|---|
| 1653 |   my ($rdns,$rfrom,$pid);
 | 
|---|
| 1654 |   if ($args{type} =~ /.i/) {
 | 
|---|
| 1655 |     ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,ip,parent_id FROM poolips WHERE id = ?",
 | 
|---|
| 1656 |         undef, ($args{id}) );
 | 
|---|
| 1657 |   } else {
 | 
|---|
| 1658 |     ($rdns, $rfrom, $pid) = $dbh->selectrow_array("SELECT rdns,cidr,parent_id FROM allocations WHERE id = ?",
 | 
|---|
| 1659 |         undef, ($args{id}) );
 | 
|---|
| 1660 |   }
 | 
|---|
| 1661 | 
 | 
|---|
| 1662 |   # Can't see a way this could end up empty, for any case I care about.  If the caller 
 | 
|---|
| 1663 |   # doesn't know an allocation ID to request, then they don't know anything else anyway.
 | 
|---|
| 1664 |   my $selfblock = $rfrom;
 | 
|---|
| 1665 | 
 | 
|---|
| 1666 |   my $type;
 | 
|---|
| 1667 |   while (!$rdns && $pid) {
 | 
|---|
| 1668 |     ($rdns, $rfrom, $pid, $type) = $dbh->selectrow_array(
 | 
|---|
| 1669 |         "SELECT rdns,cidr,parent_id,type FROM allocations WHERE id = ?",
 | 
|---|
| 1670 |         undef, ($pid) );
 | 
|---|
| 1671 |     last if $type eq 'mm';  # break loops in unfortunate legacy data
 | 
|---|
| 1672 |   }
 | 
|---|
| 1673 | 
 | 
|---|
| 1674 |   # use the actual allocation to check against the DNS utility;  we don't want
 | 
|---|
| 1675 |   # to always go chasing up the chain to the master...  which may (usually won't)
 | 
|---|
| 1676 |   # be present directly in DNS anyway
 | 
|---|
| 1677 |   my $cidr = new NetAddr::IP $selfblock;
 | 
|---|
| 1678 | 
 | 
|---|
| 1679 |   if ($rpc_url) {
 | 
|---|
| 1680 |     # Use the first /16 or /24, rather than dithering over which sub-/14 /16
 | 
|---|
| 1681 |     # or sub-/19 /24 to retrieve - it's the least-wrong way to do things.
 | 
|---|
| 1682 | 
 | 
|---|
| 1683 |     my ($rpcblock) = ($cidr->masklen <= 24 ? $cidr->split( ($cidr->masklen <= 16 ? 16 : 24) ) : $cidr);
 | 
|---|
| 1684 |     my %rpcargs = (
 | 
|---|
| 1685 |         rpcuser => $args{user},
 | 
|---|
| 1686 |         group => $revgroup,     # not sure how this could sanely be exposed, tbh...
 | 
|---|
| 1687 |         cidr => "$rpcblock",
 | 
|---|
| 1688 |         );
 | 
|---|
| 1689 | 
 | 
|---|
| 1690 |     my $remote_rdns = _rpc('getRevPattern', %rpcargs);
 | 
|---|
| 1691 |     $rdns = $remote_rdns if $remote_rdns;
 | 
|---|
| 1692 |   }
 | 
|---|
| 1693 | 
 | 
|---|
| 1694 |   # hmm.  do we care about where it actually came from?
 | 
|---|
| 1695 |   return $rdns;
 | 
|---|
| 1696 | } # end getBlockRDNS()
 | 
|---|
| 1697 | 
 | 
|---|
| 1698 | 
 | 
|---|
| 1699 | ## IPDB::getNodeList()
 | 
|---|
| 1700 | # Gets a list of node ID+name pairs as an arrayref to a list of hashrefs
 | 
|---|
| 1701 | sub getNodeList {
 | 
|---|
| 1702 |   my $dbh = shift;
 | 
|---|
| 1703 |   
 | 
|---|
| 1704 |   my $ret = $dbh->selectall_arrayref("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id",
 | 
|---|
| 1705 |         { Slice => {} });
 | 
|---|
| 1706 |   return $ret;
 | 
|---|
| 1707 | } # end getNodeList()
 | 
|---|
| 1708 | 
 | 
|---|
| 1709 | 
 | 
|---|
| 1710 | ## IPDB::getNodeName()
 | 
|---|
| 1711 | # Get node name from the ID
 | 
|---|
| 1712 | sub getNodeName {
 | 
|---|
| 1713 |   my $dbh = shift;
 | 
|---|
| 1714 |   my $nid = shift;
 | 
|---|
| 1715 | 
 | 
|---|
| 1716 |   my ($nname) = $dbh->selectrow_array("SELECT node_name FROM nodes WHERE node_id = ?", undef, ($nid) );
 | 
|---|
| 1717 |   return $nname;
 | 
|---|
| 1718 | } # end getNodeName()
 | 
|---|
| 1719 | 
 | 
|---|
| 1720 | 
 | 
|---|
| 1721 | ## IPDB::getNodeInfo()
 | 
|---|
| 1722 | # Get node name and ID associated with a block
 | 
|---|
| 1723 | sub getNodeInfo {
 | 
|---|
| 1724 |   my $dbh = shift;
 | 
|---|
| 1725 |   my $block = shift;
 | 
|---|
| 1726 | 
 | 
|---|
| 1727 |   my ($nid, $nname) = $dbh->selectrow_array("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef".
 | 
|---|
| 1728 |         " ON nodes.node_id=noderef.node_id WHERE noderef.block = ?", undef, ($block) );
 | 
|---|
| 1729 |   return ($nid, $nname);
 | 
|---|
| 1730 | } # end getNodeInfo()
 | 
|---|
| 1731 | 
 | 
|---|
| 1732 | 
 | 
|---|
| 1733 | ## IPDB::mailNotify()
 | 
|---|
| 1734 | # Sends notification mail to recipients regarding an IPDB operation
 | 
|---|
| 1735 | sub mailNotify {
 | 
|---|
| 1736 |   my $dbh = shift;
 | 
|---|
| 1737 |   my ($action,$subj,$message) = @_;
 | 
|---|
| 1738 | 
 | 
|---|
| 1739 |   return if $smtphost eq 'smtp.example.com';   # do nothing if still using default SMTP host.
 | 
|---|
| 1740 | 
 | 
|---|
| 1741 | ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases
 | 
|---|
| 1742 | 
 | 
|---|
| 1743 | # split action into parts for fiddlement.  nb: there are almost certainly better ways to do this.
 | 
|---|
| 1744 |   my @actionbits = split //, $action;
 | 
|---|
| 1745 | 
 | 
|---|
| 1746 |   # want to notify anyone who has specifically requested notify on *this* type ($action as passed),
 | 
|---|
| 1747 |   # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types",
 | 
|---|
| 1748 |   # and "all events with this action"
 | 
|---|
| 1749 |   my @actionsets = ($action);
 | 
|---|
| 1750 | ##fixme: ick, eww.  really gotta find a better way to handle this...
 | 
|---|
| 1751 |   push @actionsets, ($actionbits[0].'.'.$actionbits[2],
 | 
|---|
| 1752 |         $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/;
 | 
|---|
| 1753 | 
 | 
|---|
| 1754 |   my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain");
 | 
|---|
| 1755 | 
 | 
|---|
| 1756 |   # get recip list from db
 | 
|---|
| 1757 |   my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
 | 
|---|
| 1758 | 
 | 
|---|
| 1759 |   my %reciplist;
 | 
|---|
| 1760 |   foreach (@actionsets) {
 | 
|---|
| 1761 |     $sth->execute($_);
 | 
|---|
| 1762 | ##fixme - need to handle db errors
 | 
|---|
| 1763 |     my ($recipsub) = $sth->fetchrow_array;
 | 
|---|
| 1764 |     next if !$recipsub;
 | 
|---|
| 1765 |     foreach (split(/,/, $recipsub)) {
 | 
|---|
| 1766 |       $reciplist{$_}++;
 | 
|---|
| 1767 |     }
 | 
|---|
| 1768 |   }
 | 
|---|
| 1769 | 
 | 
|---|
| 1770 |   return if !%reciplist;
 | 
|---|
| 1771 | 
 | 
|---|
| 1772 |   foreach my $recip (keys %reciplist) {
 | 
|---|
| 1773 |     $mailer->mail("ipdb\@$domain");
 | 
|---|
| 1774 |     $mailer->to($recip);
 | 
|---|
| 1775 |     $mailer->data("From: \"$org_name IP Database\" <ipdb\@$domain>\n",
 | 
|---|
| 1776 |         "To: $recip\n",
 | 
|---|
| 1777 |         "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n",
 | 
|---|
| 1778 |         "Subject: {IPDB} $subj\n",
 | 
|---|
| 1779 |         "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n",
 | 
|---|
| 1780 |         "Organization: $org_name\n",
 | 
|---|
| 1781 |         "\n$message\n");
 | 
|---|
| 1782 |   }
 | 
|---|
| 1783 |   $mailer->quit;
 | 
|---|
| 1784 | }
 | 
|---|
| 1785 | 
 | 
|---|
| 1786 | # Indicates module loaded OK.  Required by Perl.
 | 
|---|
| 1787 | 1;
 | 
|---|