# ipdb/cgi-bin/IPDB.pm # Contains functions for IPDB - database access, subnet mangling, block allocation, etc ### # SVN revision info # $Date: 2013-10-04 20:47:32 +0000 (Fri, 04 Oct 2013) $ # SVN revision $Rev: 601 $ # Last update by $Author: kdeugau $ ### # Copyright (C) 2004-2013 Kris Deugau package IPDB; use strict; use warnings; use Exporter; use DBI; use Net::SMTP; use NetAddr::IP qw( Compact ); use POSIX; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 2; ##VERSION## @ISA = qw(Exporter); @EXPORT_OK = qw( %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist %IPDBacl %aclmsg %rpcacl &initIPDBGlobals &connectDB &finish &checkDBSanity &addMaster &touchMaster &listSummary &listMaster &listRBlock &listFree &listPool &getMasterList &getTypeList &getPoolSelect &findAllocateFrom &ipParent &subParent &blockParent &getRoutedCity &allocateBlock &updateBlock &deleteBlock &getBlockData &getNodeList &getNodeName &getNodeInfo &mailNotify ); @EXPORT = (); # Export nothing by default. %EXPORT_TAGS = ( ALL => [qw( %disp_alloctypes %list_alloctypes %def_custids @citylist @poplist %IPDBacl %aclmsg %rpcacl &initIPDBGlobals &connectDB &finish &checkDBSanity &addMaster &touchMaster &listSummary &listMaster &listRBlock &listFree &listPool &getMasterList &getTypeList &getPoolSelect &findAllocateFrom &ipParent &subParent &blockParent &getRoutedCity &allocateBlock &updateBlock &deleteBlock &getBlockData &getNodeList &getNodeName &getNodeInfo &mailNotify )] ); ## ## Global variables ## our %disp_alloctypes; our %list_alloctypes; our %def_custids; our @citylist; our @poplist; our %IPDBacl; # mapping table for functional-area => error message our %aclmsg = ( addmaster => 'add a master block', addblock => 'add an allocation', updateblock => 'update a block', delblock => 'delete an allocation', ); our %rpcacl; our $org_name = 'Example Corp'; our $smtphost = 'smtp.example.com'; our $domain = 'example.com'; our $defcustid = '5554242'; # mostly for rwhois ##fixme: leave these blank by default? our $rwhoisDataPath = '/usr/local/rwhoisd/etc/rwhoisd'; # to match ./configure defaults from rwhoisd-1.5.9.6 our $org_street = '123 4th Street'; our $org_city = 'Anytown'; our $org_prov_state = 'ON'; our $org_pocode = 'H0H 0H0'; our $org_country = 'CA'; our $org_phone = '000-555-1234'; our $org_techhandle = 'ISP-ARIN-HANDLE'; our $org_email = 'noc@example.com'; our $hostmaster = 'dns@example.com'; our $syslog_facility = 'local2'; # Allow allocations to come from private IP ranges by default? # Set to 'y' or 'on' to enable. our $allowprivrange = ''; # Let's initialize the globals. ## IPDB::initIPDBGlobals() # Initialize all globals. Takes a database handle, returns a success or error code sub initIPDBGlobals { my $dbh = $_[0]; my $sth; # Initialize alloctypes hashes $sth = $dbh->prepare("select type,listname,dispname,listorder,def_custid from alloctypes order by listorder"); $sth->execute; while (my @data = $sth->fetchrow_array) { $disp_alloctypes{$data[0]} = $data[2]; $def_custids{$data[0]} = $data[4]; if ($data[3] < 900) { $list_alloctypes{$data[0]} = $data[1]; } } # City and POP listings $sth = $dbh->prepare("select city,routing from cities order by city"); $sth->execute; return (undef,$sth->errstr) if $sth->err; while (my @data = $sth->fetchrow_array) { push @citylist, $data[0]; if ($data[1] eq 'y') { push @poplist, $data[0]; } } # Load ACL data. Specific username checks are done at a different level. $sth = $dbh->prepare("select username,acl from users"); $sth->execute; return (undef,$sth->errstr) if $sth->err; while (my @data = $sth->fetchrow_array) { $IPDBacl{$data[0]} = $data[1]; } ##fixme: initialize HTML::Template env var for template path # something like $self->path().'/templates' ? # $ENV{HTML_TEMPLATE_ROOT} = 'foo/bar'; return (1,"OK"); } # end initIPDBGlobals ## IPDB::connectDB() # Creates connection to IPDB. # Requires the database name, username, and password. # Returns a handle to the db. # Set up for a PostgreSQL db; could be any transactional DBMS with the # right changes. sub connectDB { my $dbname = shift; my $user = shift; my $pass = shift; my $dbhost = shift; my $dbh; my $DSN = "DBI:Pg:".($dbhost ? "host=$dbhost;" : '')."dbname=$dbname"; # Note that we want to autocommit by default, and we will turn it off locally as necessary. # We may not want to print gobbledygook errors; YMMV. Have to ponder that further. $dbh = DBI->connect($DSN, $user, $pass, { AutoCommit => 1, PrintError => 0 }) or return (undef, $DBI::errstr) if(!$dbh); # Return here if we can't select. Note that this indicates a # problem executing the select. my $sth = $dbh->prepare("select type from alloctypes"); $sth->execute(); return (undef,$DBI::errstr) if ($sth->err); # See if the select returned anything (or null data). This should # succeed if the select executed, but... $sth->fetchrow(); return (undef,$DBI::errstr) if ($sth->err); # If we get here, we should be OK. return ($dbh,"DB connection OK"); } # end connectDB ## IPDB::finish() # Cleans up after database handles and so on. # Requires a database handle sub finish { my $dbh = $_[0]; $dbh->disconnect if $dbh; } # end finish ## IPDB::checkDBSanity() # Quick check to see if the db is responding. A full integrity # check will have to be a separate tool to walk the IP allocation trees. sub checkDBSanity { my ($dbh) = $_[0]; if (!$dbh) { print "No database handle, or connection has been closed."; return -1; } else { # it connects, try a stmt. my $sth = $dbh->prepare("select type from alloctypes"); my $err = $sth->execute(); if ($sth->fetchrow()) { # all is well. return 1; } else { print "Connected to the database, but could not execute test statement. ".$sth->errstr(); return -1; } } # Clean up after ourselves. # $dbh->disconnect; } # end checkDBSanity ## IPDB::addMaster() # Does all the magic necessary to sucessfully add a master block # Requires database handle, block to add # Returns failure code and error message or success code and "message" sub addMaster { my $dbh = shift; my $cidr = new NetAddr::IP shift; # Allow transactions, and raise an exception on errors so we can catch it later. # Use local to make sure these get "reset" properly on exiting this block local $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; # Wrap all the SQL in a transaction eval { my ($mexist) = $dbh->selectrow_array("SELECT cidr FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) ); if (!$mexist) { # First case - master is brand-spanking-new. ##fixme: rwhois should be globally-flagable somewhere, much like a number of other things ## maybe a db table called "config"? $dbh->do("INSERT INTO masterblocks (cidr,rwhois) VALUES (?,?)", undef, ($cidr,'y') ); # Unrouted blocks aren't associated with a city (yet). We don't rely on this # elsewhere though; legacy data may have traps and pitfalls in it to break this. # Thus the "routed" flag. $dbh->do("INSERT INTO freeblocks (cidr,maskbits,city,routed) VALUES (?,?,?,?)", undef, ($cidr, $cidr->masklen, '', 'n') ); # If we get here, everything is happy. Commit changes. $dbh->commit; } # done new master does not contain existing master(s) else { # collect the master(s) we're going to absorb, and snag the longest netmask while we're at it. my $smallmask = $cidr->masklen; my $sth = $dbh->prepare("SELECT cidr FROM masterblocks WHERE cidr <<= ?"); $sth->execute($cidr); my @cmasters; while (my @data = $sth->fetchrow_array) { my $master = new NetAddr::IP $data[0]; push @cmasters, $master; $smallmask = $master->masklen if $master->masklen > $smallmask; } # split the new master, and keep only those blocks not part of an existing master my @blocklist; foreach my $seg ($cidr->split($smallmask)) { my $contained = 0; foreach my $master (@cmasters) { $contained = 1 if $master->contains($seg); } push @blocklist, $seg if !$contained; } # collect the unrouted free blocks within the new master $sth = $dbh->prepare("SELECT cidr FROM freeblocks WHERE maskbits <= ? AND cidr <<= ? AND routed = 'n'"); $sth->execute($smallmask, $cidr); while (my @data = $sth->fetchrow_array) { my $freeblock = new NetAddr::IP $data[0]; push @blocklist, $freeblock; } # combine the set of free blocks we should have now. @blocklist = Compact(@blocklist); # and now insert the new data. Make sure to delete old masters too. # freeblocks $sth = $dbh->prepare("DELETE FROM freeblocks WHERE cidr <<= ?"); my $sth2 = $dbh->prepare("INSERT INTO freeblocks (cidr,maskbits,city,routed) VALUES (?,?,'','n')"); foreach my $newblock (@blocklist) { $sth->execute($newblock); $sth2->execute($newblock, $newblock->masklen); } # master $dbh->do("DELETE FROM masterblocks WHERE cidr <<= ?", undef, ($cidr) ); $dbh->do("INSERT INTO masterblocks (cidr,rwhois) VALUES (?,?)", undef, ($cidr, 'y') ); # *whew* If we got here, we likely suceeded. $dbh->commit; } # new master contained existing master(s) }; # end eval if ($@) { my $msg = $@; eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ('OK','OK'); } } # end addMaster ## IPDB::touchMaster() # Update last-changed timestamp on a master block. sub touchMaster { my $dbh = shift; my $master = shift; local $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; eval { $dbh->do("UPDATE masterblocks SET mtime=now() WHERE cidr = ?", undef, ($master)); $dbh->commit; }; if ($@) { my $msg = $@; eval { $dbh->rollback; }; return ('FAIL',$msg); } return ('OK','OK'); } # end touchMaster() ## IPDB::listSummary() # Get summary list of all master blocks # Returns an arrayref to a list of hashrefs containing the master block, routed count, # allocated count, free count, and largest free block masklength sub listSummary { my $dbh = shift; my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master FROM masterblocks ORDER BY cidr", { Slice => {} }); foreach (@{$mlist}) { my ($rcnt) = $dbh->selectrow_array("SELECT count(*) FROM routed WHERE cidr <<= ?", undef, ($$_{master})); $$_{routed} = $rcnt; my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ?", undef, ($$_{master})); $$_{allocated} = $acnt; my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?". " AND (routed='y' OR routed='n')", undef, ($$_{master})); $$_{free} = $fcnt; my ($bigfree) = $dbh->selectrow_array("SELECT maskbits FROM freeblocks WHERE cidr <<= ?". " AND (routed='y' OR routed='n') ORDER BY maskbits LIMIT 1", undef, ($$_{master})); ##fixme: should find a way to do this without having to HTMLize the <> $bigfree = "/$bigfree" if $bigfree; $bigfree = '' if !$bigfree; $$_{bigfree} = $bigfree; } return $mlist; } # end listSummary() ## IPDB::listMaster() # Get list of routed blocks in the requested master # Returns an arrayref to a list of hashrefs containing the routed block, POP/city the block is routed to, # allocated count, free count, and largest free block masklength sub listMaster { my $dbh = shift; my $master = shift; my $rlist = $dbh->selectall_arrayref("SELECT cidr AS block,city FROM routed WHERE cidr <<= ? ORDER BY cidr", { Slice => {} }, ($master) ); foreach (@{$rlist}) { my ($acnt) = $dbh->selectrow_array("SELECT count(*) FROM allocations WHERE cidr <<= ?", undef, ($$_{block})); $$_{nsubs} = $acnt; my ($fcnt) = $dbh->selectrow_array("SELECT count(*) FROM freeblocks WHERE cidr <<= ?". " AND (routed='y' OR routed='n')", undef, ($$_{block})); $$_{nfree} = $fcnt; my ($bigfree) = $dbh->selectrow_array("SELECT maskbits FROM freeblocks WHERE cidr <<= ?". " AND (routed='y' OR routed='n') ORDER BY maskbits LIMIT 1", undef, ($$_{block})); ##fixme: should find a way to do this without having to HTMLize the <> $bigfree = "/$bigfree" if $bigfree; $bigfree = '' if !$bigfree; $$_{lfree} = $bigfree; } return $rlist; } # end listMaster() ## IPDB::listRBlock() # Gets a list of free blocks in the requested parent/master in both CIDR and range notation # Takes a parent/master and an optional flag to look at routed or unrouted blocks, depending # on whether the master is a direct master or a routed block # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks sub listRBlock { my $dbh = shift; my $routed = shift; # Snag the allocations for this block my $sth = $dbh->prepare("SELECT cidr,city,type,custid,swip,description,vrf". " FROM allocations WHERE cidr <<= ? ORDER BY cidr"); $sth->execute($routed); # hack hack hack # set up to flag swip=y records if they don't actually have supporting data in the customers table my $custsth = $dbh->prepare("SELECT count(*) FROM customers WHERE custid = ?"); my @blocklist; while (my ($cidr,$city,$type,$custid,$swip,$desc,$vrf) = $sth->fetchrow_array()) { $desc .= " - vrf:$vrf" if $desc && $vrf; $desc = "vrf:$vrf" if !$desc && $vrf; $custsth->execute($custid); my ($ncust) = $custsth->fetchrow_array(); my %row = ( block => $cidr, city => $city, type => $disp_alloctypes{$type}, custid => $custid, swip => ($swip eq 'y' ? 'Yes' : 'No'), partswip => ($swip eq 'y' && $ncust == 0 ? 1 : 0), desc => $desc ); $row{subblock} = ($type =~ /^.r$/); # hmf. wonder why these won't work in the hash declaration... $row{listpool} = ($type =~ /^.[pd]$/); push (@blocklist, \%row); } return \@blocklist; } # end listRBlock() ## IPDB::listFree() # Gets a list of free blocks in the requested parent/master in both CIDR and range notation # Takes a parent/master and an optional "routed or unrouted" flag that defaults to unrouted. # Returns an arrayref to a list of hashrefs containing the CIDR and range-notation blocks # Returns some extra flags in the hashrefs for routed blocks, since those can have several subtypes sub listFree { my $dbh = shift; my $master = shift; my $routed = shift || 'n'; # do it this way so we can waste a little less time iterating my $sth = $dbh->prepare("SELECT cidr,routed FROM freeblocks WHERE cidr <<= ? AND ". ($routed eq 'n' ? '' : 'NOT')." routed = 'n' ORDER BY cidr"); $sth->execute($master); my @flist; while (my ($cidr,$rtype) = $sth->fetchrow_array()) { $cidr = new NetAddr::IP $cidr; my %row = ( fblock => "$cidr", frange => $cidr->range, ); if ($routed eq 'y') { $row{subblock} = ($rtype ne 'y' && $rtype ne 'n'); $row{fbtype} = $rtype; } push @flist, \%row; } return \@flist; } # end listFree() ## IPDB::listPool() # sub listPool { my $dbh = shift; my $pool = shift; my $sth = $dbh->prepare("SELECT ip,custid,available,description,type,vrf". " FROM poolips WHERE pool = ? ORDER BY ip"); $sth->execute($pool); my @poolips; while (my ($ip,$custid,$available,$desc,$type,$vrf) = $sth->fetchrow_array) { $desc .= " - vrf:$vrf" if $desc && $vrf; $desc = "vrf:$vrf" if !$desc && $vrf; my %row = ( ip => $ip, custid => $custid, available => $available, desc => $desc, delme => $available eq 'n' ); push @poolips, \%row; } return \@poolips; } # end listPool() ## IPDB::getMasterList() # Get a list of master blocks, optionally including last-modified timestamps # Takes an optional flag to indicate whether to include timestamps; # 'm' includes ctime, all others (suggest 'c') do not. # Returns an arrayref to a list of hashrefs sub getMasterList { my $dbh = shift; my $stampme = shift || 'm'; # optional but should be set by caller for clarity my $mlist = $dbh->selectall_arrayref("SELECT cidr AS master".($stampme eq 'm' ? ',mtime' : ''). " FROM masterblocks ORDER BY cidr", { Slice => {} }); return $mlist; } # end getMasterList() ## IPDB::getTypeList() # Get an alloctype/description pair list suitable for dropdowns # Takes a flag to determine which general groups of types are returned # Returns an reference to an array of hashrefs sub getTypeList { my $dbh = shift; my $tgroup = shift || 'a'; # technically optional, like this, but should # really be specified in the call for clarity my $tlist; if ($tgroup eq 'n') { # grouping 'n' - all netblock types. These include routed blocks, containers (_c) # and contained (_r) types, dynamic-allocation ranges (_e), static IP pools (_d and _p), # and the "miscellaneous" cn, in, and en types. $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ". "AND type NOT LIKE '_i' ORDER BY listorder", { Slice => {} }); } elsif ($tgroup eq 'p') { # grouping 'p' - primary allocation types. As with 'n' above but without the _r contained types. $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ". "AND type NOT LIKE '_i' AND type NOT LIKE '_r' ORDER BY listorder", { Slice => {} }); } elsif ($tgroup eq 'c') { # grouping 'c' - contained types. These include all static IPs and all _r types. $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ". " AND (type LIKE '_i' OR type LIKE '_r') ORDER BY listorder", { Slice => {} }); } elsif ($tgroup eq 'i') { # grouping 'i' - static IP types. $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ". " AND type LIKE '_i' ORDER BY listorder", { Slice => {} }); } else { # grouping 'a' - all standard allocation types. This includes everything # but mm (present only as a formality). Make this the default. $tlist = $dbh->selectall_arrayref("SELECT type,listname FROM alloctypes WHERE listorder <= 500 ". " ORDER BY listorder", { Slice => {} }); } return $tlist; } ## IPDB::getPoolSelect() # Get a list of pools matching the passed city and type that have 1 or more free IPs # Returns an arrayref to a list of hashrefs sub getPoolSelect { my $dbh = shift; my $iptype = shift; my $pcity = shift; my ($ptype) = ($iptype =~ /^(.)i$/); return if !$ptype; $ptype .= '_'; my $plist = $dbh->selectall_arrayref( "SELECT (SELECT city FROM allocations WHERE cidr=poolips.pool) AS poolcit, ". "poolips.pool AS poolblock, COUNT(*) AS poolfree FROM poolips,allocations ". "WHERE poolips.available='y' AND poolips.pool=allocations.cidr ". "AND allocations.city = ? AND poolips.type LIKE ? ". "GROUP BY pool", { Slice => {} }, ($pcity, $ptype) ); return $plist; } # end getPoolSelect() ## IPDB::findAllocateFrom() # Find free block to add a new allocation from. (CIDR block version of pool select above, more or less) # Takes # - mask length # - allocation type # - POP city "parent" # - optional master-block restriction # - optional flag to allow automatic pick-from-private-network-ranges # Returns a string with the first CIDR block matching the criteria, if any sub findAllocateFrom { my $dbh = shift; my $maskbits = shift; my $type = shift; my $city = shift; my $pop = shift; my %optargs = @_; my $failmsg = "No suitable free block found\n"; ## Set up the SQL to find out what freeblock we can (probably) use for an allocation. ## Very large systems will require development of a reserve system (possibly an extension ## of the reserve-for-expansion concept in https://secure.deepnet.cx/trac/ipdb/ticket/24?) ## Also populate a value list for the DBI call. my @vallist = ($maskbits, ($type eq 'rm' ? 'n' : ($type =~ /^(.)r$/ ? "$1" : 'y')) ); my $sql = "SELECT cidr FROM freeblocks WHERE maskbits <= ? AND routed = ?"; # for PPP(oE) and container types, the POP city is the one attached to the pool. # individual allocations get listed with the customer city site. ##fixme: chain cities to align roughly with a full layer-2 node graph $city = $pop if $type !~ /^.[pc]$/; if ($type ne 'rm' && $city) { $sql .= " AND city = ?"; push @vallist, $city; } # Allow specifying an arbitrary full block, instead of a master if ($optargs{gimme}) { $sql .= " AND cidr >>= ?"; push @vallist, $optargs{gimme}; } # if a specific master was requested, allow the requestor to self->shoot(foot) if ($optargs{master} && $optargs{master} ne '-') { $sql .= " AND cidr <<= ?" if $optargs{master} ne '-'; push @vallist, $optargs{master}; } else { # if a specific master was NOT requested, filter out the RFC 1918 private networks if (!$optargs{allowpriv}) { $sql .= " AND NOT (cidr <<= '192.168.0.0/16' OR cidr <<= '10.0.0.0/8' OR cidr <<= '172.16.0.0/12')"; } } # Sorting and limiting, since we don't (currently) care to provide a selection of # blocks to carve up. This preserves something resembling optimal usage of the IP # space by forcing contiguous allocations and free blocks as much as possible. $sql .= " ORDER BY maskbits DESC,cidr LIMIT 1"; my ($fbfound) = $dbh->selectrow_array($sql, undef, @vallist); return $fbfound; } # end findAllocateFrom() ## IPDB::ipParent() # Get an IP's parent pool's details # Takes a database handle and IP # Returns a hashref to the parent pool block, if any sub ipParent { my $dbh = shift; my $block = shift; my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations". " WHERE cidr >>= ?", undef, ($block) ); return $pinfo; } # end ipParent() ## IPDB::subParent() # Get a block's parent's details # Takes a database handle and CIDR block # Returns a hashref to the parent container block, if any sub subParent { my $dbh = shift; my $block = shift; my $pinfo = $dbh->selectrow_hashref("SELECT cidr,custid,type,city,description FROM allocations". " WHERE cidr >>= ?", undef, ($block) ); return $pinfo; } # end subParent() ## IPDB::blockParent() # Get a block's parent's details # Takes a database handle and CIDR block # Returns a hashref to the parent container block, if any sub blockParent { my $dbh = shift; my $block = shift; my $pinfo = $dbh->selectrow_hashref("SELECT cidr,city FROM routed". " WHERE cidr >>= ?", undef, ($block) ); return $pinfo; } # end blockParent() ## IPDB::getRoutedCity() # Get the city for a routed block. sub getRoutedCity { my $dbh = shift; my $block = shift; my ($rcity) = $dbh->selectrow_array("SELECT city FROM routed WHERE cidr = ?", undef, ($block) ); return $rcity; } # end getRoutedCity() ## IPDB::allocateBlock() # Does all of the magic of actually allocating a netblock # Requires database handle, block to allocate, custid, type, city, # description, notes, circuit ID, block to allocate from, private data # Returns a success code and optional error message. sub allocateBlock { my ($dbh,undef,undef,$custid,$type,$city,$desc,$notes,$circid,$privdata,$nodeid,$vrf) = @_; $privdata = '' if !defined($privdata); $vrf = '' if !defined($vrf); my $cidr = new NetAddr::IP $_[1]; my $alloc_from = new NetAddr::IP $_[2]; my $sth; $desc = '' if !$desc; $notes = '' if !$notes; $circid = '' if !$circid; $privdata = '' if !$privdata; # Snag the "type" of the freeblock (alloc_from) "just in case" $sth = $dbh->prepare("select routed from freeblocks where cidr='$alloc_from'"); $sth->execute; my ($alloc_from_type) = $sth->fetchrow_array; # To contain the error message, if any. my $msg = "Unknown error allocating $cidr as '$type'"; # Enable transactions and error handling local $dbh->{AutoCommit} = 0; # These need to be local so we don't local $dbh->{RaiseError} = 1; # step on our toes by accident. if ($type =~ /^.i$/) { $msg = "Unable to assign static IP $cidr to $custid"; eval { # We have to do this in two parts because otherwise we lose # the ability to return the IP assigned. Should that change, # the commented SQL statement below may become usable. # update poolips set custid='$custid',city='$city',available='n', # description='$desc',notes='$notes',circuitid='$circid' # where ip=(select ip from poolips where pool='$alloc_from' # and available='y' order by ip limit 1); if ($cidr) { my ($isavail) = $dbh->selectrow_array("SELECT available FROM poolips WHERE ip=?", undef, ($cidr) ); if ($isavail eq 'n') { die "IP already allocated. Deallocate and reallocate, or update the entry\n"; } if (!$isavail) { die "IP is not in an IP pool.\n"; } } else { ($cidr) = $dbh->selectrow_array("SELECT ip FROM poolips WHERE pool=? AND available='y' ORDER BY ip", undef, ($alloc_from) ); } $dbh->do("UPDATE poolips SET custid=?,city=?,available='n',description=?,". "notes=?,circuitid=?,privdata=?,vrf=? ". "WHERE ip=?", undef, ($custid, $city, $desc, $notes, $circid, $privdata, $vrf, $cidr) ); # node hack if ($nodeid && $nodeid ne '') { $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)"); $sth->execute("$cidr",$nodeid); } # end node hack $dbh->commit; }; if ($@) { $msg .= ": $@"; eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ('OK',"$cidr"); } } else { # end IP-from-pool allocation my $errcode = 'OK'; if ($cidr == $alloc_from) { # Easiest case- insert in one table, delete in the other, and go home. More or less. # insert into allocations values (cidr,custid,type,city,desc) and # delete from freeblocks where cidr='cidr' # For data safety on non-transaction DBs, we delete first. eval { $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'"; if ($type eq 'rm') { $sth = $dbh->prepare("update freeblocks set routed='y',city='$city'". " where cidr='$cidr'"); $sth->execute; $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)". " values ('$cidr',".$cidr->masklen.",'$city')"); $sth->execute; } else { # common stuff for end-use, dialup, dynDSL, pools, etc, etc. # special case - block is a container/"reserve" block if ($type =~ /^(.)c$/) { $sth = $dbh->prepare("update freeblocks set routed='$1' where cidr='$cidr'"); $sth->execute; } else { # "normal" case $sth = $dbh->prepare("delete from freeblocks where cidr='$cidr'"); $sth->execute; } $sth = $dbh->prepare("insert into allocations". " (cidr,custid,type,city,description,notes,maskbits,circuitid,privdata,vrf)". " values (?,?,?,?,?,?,?,?,?,?)"); $sth->execute("$cidr", $custid, $type, $city, $desc, $notes, $cidr->masklen, $circid, $privdata, $vrf); # And initialize the pool, if necessary # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed. if ($type =~ /^.p$/) { $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr"; my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all"); die $rmsg if $code eq 'FAIL'; $msg = $rmsg; $errcode = $code; } elsif ($type =~ /^.d$/) { $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr"; my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal"); die $rmsg if $code eq 'FAIL'; $msg = $rmsg; $errcode = $code; } } # routing vs non-routing netblock # node hack if ($nodeid && $nodeid ne '') { $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)"); $sth->execute("$cidr",$nodeid); } # end node hack $dbh->commit; }; # end of eval if ($@) { $msg .= ": ".$@; eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ($errcode,($type =~ /^.[pd]$/ ? $msg : "OK")); } } else { # cidr != alloc_from # Hard case. Allocation is smaller than free block. my $wantmaskbits = $cidr->masklen; my $maskbits = $alloc_from->masklen; my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock. # This determines which blocks will be left "free" after allocation. We take the # block we're allocating from, and split it in half. We see which half the wanted # block is in, and repeat until the wanted block is equal to one of the halves. my $i=0; my $tmp_from = $alloc_from; # So we don't munge $alloc_from while ($maskbits++ < $wantmaskbits) { my @subblocks = $tmp_from->split($maskbits); $newfreeblocks[$i++] = (($cidr->within($subblocks[0])) ? $subblocks[1] : $subblocks[0]); $tmp_from = ( ($cidr->within($subblocks[0])) ? $subblocks[0] : $subblocks[1] ); } # while # Begin SQL transaction block eval { $msg = "Unable to allocate $cidr as '$disp_alloctypes{$type}'"; # Delete old freeblocks entry $sth = $dbh->prepare("delete from freeblocks where cidr='$alloc_from'"); $sth->execute(); # now we have to do some magic for routing blocks if ($type eq 'rm') { # Insert the new freeblocks entries # Note that non-routed blocks are assigned to # and use the default value for the routed column ('n') $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)". " values (?, ?, '')"); foreach my $block (@newfreeblocks) { $sth->execute("$block", $block->masklen); } # Insert the entry in the routed table $sth = $dbh->prepare("insert into routed (cidr,maskbits,city)". " values ('$cidr',".$cidr->masklen.",'$city')"); $sth->execute; # Insert the (almost) same entry in the freeblocks table $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". " values ('$cidr',".$cidr->masklen.",'$city','y')"); $sth->execute; } else { # done with alloctype == rm # Insert the new freeblocks entries # Along with some more HairyPerl(TM): # if $alloc_type_from is p # OR # $type matches /^(.)r$/ # inserted value for routed column should match. # This solves the case of inserting an arbitrary block into a # "Reserve-for-routed-DSL" block. Which you really shouldn't # do in the first place, but anyway... $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". " values (?, ?, (select city from routed where cidr >>= '$cidr'),'". ( ( ($alloc_from_type =~ /^(p)$/) || ($type =~ /^(.)r$/) ) ? "$1" : 'y')."')"); foreach my $block (@newfreeblocks) { $sth->execute("$block", $block->masklen); } # Special-case for reserve/"container" blocks - generate # the "extra" freeblocks entry for the container if ($type =~ /^(.)c$/) { $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". " values ('$cidr',".$cidr->masklen.",'$city','$1')"); $sth->execute; } # Insert the allocations entry $sth = $dbh->prepare("insert into allocations (cidr,custid,type,city,". "description,notes,maskbits,circuitid,privdata,vrf)". " values (?,?,?,?,?,?,?,?,?,?)"); $sth->execute("$cidr", $custid, $type, $city, $desc, $notes, $cidr->masklen, $circid, $privdata, $vrf); # And initialize the pool, if necessary # PPPoE pools (currently dialup, DSL, and WiFi) get all IPs made available # "DHCP" or "real-subnet" pools have the net, gw, and bcast IPs removed. if ($type =~ /^.p$/) { $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr"; my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"all"); die $rmsg if $code eq 'FAIL'; } elsif ($type =~ /^.d$/) { $msg = "Could not initialize IPs in new $disp_alloctypes{$type} $cidr"; my ($code,$rmsg) = initPool($dbh,$cidr,$type,$city,"normal"); die $rmsg if $code eq 'FAIL'; } } # done with netblock alloctype != rm # node hack if ($nodeid && $nodeid ne '') { $sth = $dbh->prepare("INSERT INTO noderef (block,node_id) VALUES (?,?)"); $sth->execute("$cidr",$nodeid); } # end node hack $dbh->commit; }; # end eval if ($@) { $msg .= ": ".$@; eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ($errcode,($type =~ /^.[pd]$/ ? $msg : "OK")); } } # end fullcidr != alloc_from } # end static-IP vs netblock allocation } # end allocateBlock() ## IPDB::initPool() # Initializes a pool # Requires a database handle, the pool CIDR, type, city, and a parameter # indicating whether the pool should allow allocation of literally every # IP, or if it should reserve network/gateway/broadcast IPs # Note that this is NOT done in a transaction, that's why it's a private # function and should ONLY EVER get called from allocateBlock() sub initPool { my ($dbh,undef,$type,$city,$class) = @_; my $pool = new NetAddr::IP $_[1]; return ('WARN','Refusing to melt server with IPv6 IP pool') if $pool->bits == 128; ##fixme Need to just replace 2nd char of type with i rather than capturing 1st char of type $type =~ s/[pd]$/i/; my $sth; my $msg; # Trap errors so we can pass them back to the caller. Even if the # caller is only ever supposed to be local, and therefore already # trapping errors. >:( local $dbh->{AutoCommit} = 0; # These need to be local so we don't local $dbh->{RaiseError} = 1; # step on our toes by accident. eval { # have to insert all pool IPs into poolips table as "unallocated". $sth = $dbh->prepare("insert into poolips (pool,ip,custid,city,type)". " values ('$pool', ?, '$defcustid', ?, '$type')"); my @poolip_list = $pool->hostenum; if ($class eq 'all') { # (DSL-ish block - *all* IPs available if ($pool->addr !~ /\.0$/) { # .0 causes weirdness. $sth->execute($pool->addr, $city); } for (my $i=0; $i<=$#poolip_list; $i++) { $sth->execute($poolip_list[$i]->addr, $city); } $pool--; if ($pool->addr !~ /\.255$/) { # .255 can cause weirdness. $sth->execute($pool->addr, $city); } } else { # (real netblock) for (my $i=1; $i<=$#poolip_list; $i++) { $sth->execute($poolip_list[$i]->addr, $city); } } }; if ($@) { $msg = $@." '".$sth->errstr."'"; eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ('OK',"OK"); } } # end initPool() ## IPDB::updateBlock() # Update an allocation # Takes all allocation fields in a hash sub updateBlock { my $dbh = shift; my %args = @_; return ('FAIL', 'Missing block to update') if !$args{block}; # do it all in a transaction local $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; my @fieldlist; my @vallist; foreach ('custid', 'city', 'description', 'notes', 'circuitid', 'privdata', 'vrf') { if ($args{$_}) { push @fieldlist, $_; push @vallist, $args{$_}; } } my $updtable = 'allocations'; my $keyfield = 'cidr'; if ($args{type} =~ /^(.)i$/) { $updtable = 'poolips'; $keyfield = 'ip'; } else { ## fixme: there's got to be a better way... if ($args{swip}) { if ($args{swip} eq 'on' || $args{swip} eq '1' || $args{swip} eq 'y') { $args{swip} = 'y'; } else { $args{swip} = 'n'; } } foreach ('type', 'swip') { if ($args{$_}) { push @fieldlist, $_; push @vallist, $args{$_}; } } } return ('FAIL', 'No fields to update') if !@fieldlist; push @vallist, $args{block}; my $sql = "UPDATE $updtable SET "; $sql .= join " = ?, ", @fieldlist; $sql .= " = ? WHERE $keyfield = ?"; eval { # do the update $dbh->do($sql, undef, @vallist); if ($args{node}) { # done with delete/insert so we don't have to worry about funkyness updating a node ref that isn't there $dbh->do("DELETE FROM noderef WHERE block = ?", undef, ($args{block}) ); $dbh->do("INSERT INTO noderef (block,node_id) VALUES (?,?)", undef, ($args{block}, $args{node}) ); } $dbh->commit; }; if ($@) { my $msg = $@; $dbh->rollback; return ('FAIL', $msg); } return 0; } # end updateBlock() ## IPDB::deleteBlock() # Removes an allocation from the database, including deleting IPs # from poolips and recombining entries in freeblocks if possible # Also handles "deleting" a static IP allocation, and removal of a master # Requires a database handle, the block to delete, and the type of block sub deleteBlock { my ($dbh,undef,$type) = @_; my $cidr = new NetAddr::IP $_[1]; my $sth; # Magic variables used for odd allocation cases. my $container; my $con_type; # To contain the error message, if any. my $msg = "Unknown error deallocating $type $cidr"; # Enable transactions and exception-on-errors... but only for this sub local $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; # First case. The "block" is a static IP # Note that we still need some additional code in the odd case # of a netblock-aligned contiguous group of static IPs if ($type =~ /^.i$/) { eval { $msg = "Unable to deallocate $disp_alloctypes{$type} $cidr"; $sth = $dbh->prepare("update poolips set custid=?,available='y',". "city=(select city from allocations where cidr >>= ?". " order by masklen(cidr) desc limit 1),". "description='',notes='',circuitid='' where ip=?"); $sth->execute($defcustid, "$cidr", "$cidr"); $dbh->commit; }; if ($@) { eval { $dbh->rollback; }; return ('FAIL',$msg); } else { return ('OK',"OK"); } } elsif ($type eq 'mm') { # end alloctype =~ /.i/ $msg = "Unable to delete master block $cidr"; eval { $sth = $dbh->prepare("delete from masterblocks where cidr=?"); $sth->execute($cidr); $sth = $dbh->prepare("delete from freeblocks where cidr <<= ?"); $sth->execute($cidr); $dbh->commit; }; if ($@) { eval { $dbh->rollback; }; return ('FAIL', $msg); } else { return ('OK',"OK"); } } else { # end alloctype master block case ## This is a big block; but it HAS to be done in a chunk. Any removal ## of a netblock allocation may result in a larger chunk of free ## contiguous IP space - which may in turn be combined into a single ## netblock rather than a number of smaller netblocks. eval { if ($type eq 'rm') { $msg = "Unable to remove routing allocation $cidr"; $sth = $dbh->prepare("delete from routed where cidr='$cidr'"); $sth->execute; # Make sure block getting deleted is properly accounted for. $sth = $dbh->prepare("update freeblocks set routed='n',city=''". " where cidr='$cidr'"); $sth->execute; # Set up query to start compacting free blocks. $sth = $dbh->prepare("select cidr from freeblocks where ". "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc"); } else { # end alloctype routing case # Magic. We need to get information about the containing block (if any) # so as to make sure that the freeblocks we insert get the correct "type". $sth = $dbh->prepare("select cidr,type from allocations where cidr >> '$cidr'"); $sth->execute; ($container, $con_type) = $sth->fetchrow_array; # Delete all allocations within the block being deleted. This is # deliberate and correct, and removes the need to special-case # removal of "container" blocks. $sth = $dbh->prepare("delete from allocations where cidr <<='$cidr'"); $sth->execute; # Special case - delete pool IPs if ($type =~ /^.[pd]$/) { # We have to delete the IPs from the pool listing. $sth = $dbh->prepare("delete from poolips where pool='$cidr'"); $sth->execute; } # Set up query for compacting free blocks. if ($con_type && $con_type eq 'pc') { # Clean up after "bad" allocations (blocks that are not formally # contained which have nevertheless been allocated from a container block) # We want to make certain that the freeblocks are properly "labelled" $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= '$container' order by maskbits desc"); } else { # Standard deallocation. $sth = $dbh->prepare("select cidr from freeblocks where cidr <<= ". "(select cidr from routed where cidr >>= '$cidr') ". " and maskbits<=".$cidr->masklen. " and routed='".(($type =~ /^(.)r$/) ? "$1" : 'y'). "' order by maskbits desc"); } } # end alloctype general case ## Deallocate legacy blocks stashed in the middle of a static IP pool ## This may be expandable to an even more general case of contained netblock, or other pool types. # Find out if the block we're deallocating is within a DSL pool my $sth2 = $dbh->prepare("SELECT cidr,city,type FROM allocations WHERE type LIKE '_p' AND cidr >>= ?"); $sth2->execute("$cidr"); my ($pool,$poolcity,$pooltype) = $sth2->fetchrow_array; if ($pool || $sth2->rows) { # We've already deleted the block, now we have to stuff its IPs into the pool. $pooltype =~ s/p$/i/; # change type to static IP $sth2 = $dbh->prepare("INSERT INTO poolips (pool,ip,city,type,custid) values ". "('$pool',?,'$poolcity','$pooltype','$defcustid')"); ##fixme: need to not insert net, gateway, and bcast on "real netblock" pools (DHCPish) # don't insert .0 $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.0$|; foreach my $ip ($cidr->hostenum) { $sth2->execute("$ip"); } $cidr--; # don't insert .255 $sth2->execute($cidr->addr) unless $cidr->addr =~ m|\.255$|; } else { # done returning IPs from a block to a static DSL pool # Now we look for larger-or-equal-sized free blocks in the same master (routed) # (super)block. If there aren't any, we can't combine blocks anyway. If there # are, we check to see if we can combine blocks. # Execute the statement prepared in the if-else above. $sth->execute; # NetAddr::IP->compact() attempts to produce the smallest inclusive block # from the caller and the passed terms. # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2, # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63, # .64-.95, and .96-.128), you will get an array containing a single # /25 as element 0 (.0-.127). Order is not important; you could have # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27. my (@together, @combinelist); my $i=0; while (my @data = $sth->fetchrow_array) { my $testIP = new NetAddr::IP $data[0]; @together = $testIP->compact($cidr); my $num = @together; if ($num == 1) { $cidr = $together[0]; $combinelist[$i++] = $testIP; } } # Clear old freeblocks entries - if any. They should all be within # the $cidr determined above. $sth = $dbh->prepare("delete from freeblocks where cidr <<='$cidr'"); $sth->execute; # insert "new" freeblocks entry if ($type eq 'rm') { $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city)". " values ('$cidr',".$cidr->masklen.",'')"); } else { # Magic hackery to insert "correct" data for deallocation of # non-contained blocks allocated from within a container. $type = 'pr' if $con_type && $con_type eq 'pc'; $sth = $dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". " values ('$cidr',".$cidr->masklen. ",(select city from routed where cidr >>= '$cidr'),'". (($type =~ /^(.)r$/) ? "$1" : 'y')."')"); } $sth->execute; } # done returning IPs to the appropriate place # If we got here, we've succeeded. Whew! $dbh->commit; }; # end eval if ($@) { $msg = $@; eval { $dbh->rollback; }; return ('FAIL', $msg); } else { return ('OK',"OK"); } } # end alloctype != netblock } # end deleteBlock() ## IPDB::getBlockData() # Get CIDR or IP, custid, type, city, circuit ID, description, notes, modification time, private/restricted # data, and VRF tag, for a CIDR block or pool IP # Also returns SWIP status flag for CIDR blocks # Takes the block/IP to look up # Returns an arrayref to a list of hashrefs sub getBlockData { my $dbh = shift; my $block = shift; my $cidr = new NetAddr::IP $block; my $keycol = 'cidr'; my $blocktable = 'allocations'; my $poolip = 0; # Pool IP and IPv6 check all in one! Still needs to be tightened # up a little for the as-yet-unhandled case of IPv6 IP pools if ($cidr->bits == 32 && $cidr->masklen == 32) { $poolip = 1; $keycol = 'ip'; $blocktable = 'poolips'; } my $binfo = $dbh->selectrow_hashref("SELECT $keycol AS block, custid, type, city, circuitid, description,". " notes, modifystamp AS lastmod, privdata, vrf".($poolip ? '' : ', swip')." FROM $blocktable". " WHERE $keycol = ?", undef, ($block) ); return $binfo; } # end getBlockData() ## IPDB::getNodeList() # Gets a list of node ID+name pairs as an arrayref to a list of hashrefs sub getNodeList { my $dbh = shift; my $ret = $dbh->selectall_arrayref("SELECT node_id, node_name FROM nodes ORDER BY node_type,node_id", { Slice => {} }); return $ret; } # end getNodeList() ## IPDB::getNodeName() # Get node name from the ID sub getNodeName { my $dbh = shift; my $nid = shift; my ($nname) = $dbh->selectrow_array("SELECT node_name FROM nodes WHERE node_id = ?", undef, ($nid) ); return $nname; } # end getNodeName() ## IPDB::getNodeInfo() # Get node name and ID associated with a block sub getNodeInfo { my $dbh = shift; my $block = shift; my ($nid, $nname) = $dbh->selectrow_array("SELECT nodes.node_id,node_name FROM nodes INNER JOIN noderef". " ON nodes.node_id=noderef.node_id WHERE noderef.block = ?", undef, ($block) ); return ($nid, $nname); } # end getNodeInfo() ## IPDB::mailNotify() # Sends notification mail to recipients regarding an IPDB operation sub mailNotify { my $dbh = shift; my ($action,$subj,$message) = @_; return if $smtphost eq 'smtp.example.com'; # do nothing if still using default SMTP host. ##fixme: need to redesign the breakdown/processing for $action for proper handling of all cases # split action into parts for fiddlement. nb: there are almost certainly better ways to do this. my @actionbits = split //, $action; # want to notify anyone who has specifically requested notify on *this* type ($action as passed), # on "all static IP types" or "all pool types" (and other last-char-in-type groupings), on eg "all DSL types", # and "all events with this action" my @actionsets = ($action); ##fixme: ick, eww. really gotta find a better way to handle this... push @actionsets, ($actionbits[0].'.'.$actionbits[2], $actionbits[0].$actionbits[1].'.', $actionbits[0].'a') if $action =~ /^.{3}$/; my $mailer = Net::SMTP->new($smtphost, Hello => "ipdb.$domain"); # get recip list from db my $sth = $dbh->prepare("SELECT reciplist FROM notify WHERE action=?"); my %reciplist; foreach (@actionsets) { $sth->execute($_); ##fixme - need to handle db errors my ($recipsub) = $sth->fetchrow_array; next if !$recipsub; foreach (split(/,/, $recipsub)) { $reciplist{$_}++; } } return if !%reciplist; foreach my $recip (keys %reciplist) { $mailer->mail("ipdb\@$domain"); $mailer->to($recip); $mailer->data("From: \"$org_name IP Database\" \n", "To: $recip\n", "Date: ".strftime("%a, %d %b %Y %H:%M:%S %z",localtime)."\n", "Subject: {IPDB} $subj\n", "X-Mailer: IPDB Notify v".sprintf("%.1d",$IPDB::VERSION)."\n", "Organization: $org_name\n", "\n$message\n"); } $mailer->quit; } # Indicates module loaded OK. Required by Perl. 1;