source: trunk/cgi-bin/IPDB.pm@ 626

Last change on this file since 626 was 625, checked in by Kris Deugau, 10 years ago

/trunk

Commit 1/mumble for work done intermittently over the past ~year.
See #5, comment 25:

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