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