1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/main.cgi
|
---|
3 | # Started munging from noc.vianet's old IPDB 04/22/2004
|
---|
4 | ###
|
---|
5 | # SVN revision info
|
---|
6 | # $Date: 2005-01-26 22:05:46 +0000 (Wed, 26 Jan 2005) $
|
---|
7 | # SVN revision $Rev: 136 $
|
---|
8 | # Last update by $Author: kdeugau $
|
---|
9 | ###
|
---|
10 |
|
---|
11 | use strict;
|
---|
12 | use warnings;
|
---|
13 | use CGI::Carp qw(fatalsToBrowser);
|
---|
14 | use DBI;
|
---|
15 | use CommonWeb qw(:ALL);
|
---|
16 | use IPDB 2.0 qw(:ALL);
|
---|
17 | use CustIDCK;
|
---|
18 | use POSIX qw(ceil);
|
---|
19 | use NetAddr::IP;
|
---|
20 |
|
---|
21 | use Sys::Syslog;
|
---|
22 |
|
---|
23 | openlog "IPDB","pid","local2";
|
---|
24 |
|
---|
25 | # Collect the username from HTTP auth. If undefined, we're in a test environment.
|
---|
26 | my $authuser;
|
---|
27 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
28 | $authuser = '__temptest';
|
---|
29 | } else {
|
---|
30 | $authuser = $ENV{'REMOTE_USER'};
|
---|
31 | }
|
---|
32 |
|
---|
33 | syslog "debug", "$authuser active";
|
---|
34 |
|
---|
35 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
36 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
37 | my $ip_dbh;
|
---|
38 | my $sth;
|
---|
39 | my $errstr;
|
---|
40 | ($ip_dbh,$errstr) = connectDB("ipdb", "ipdb", "ipdbpwd");
|
---|
41 | if (!$ip_dbh) {
|
---|
42 | printAndExit("Failed to connect to database: $errstr\n");
|
---|
43 | }
|
---|
44 | checkDBSanity($ip_dbh);
|
---|
45 | initIPDBGlobals($ip_dbh);
|
---|
46 |
|
---|
47 | #prototypes
|
---|
48 | sub viewBy($$); # feed it the category and query
|
---|
49 | sub queryResults($$$); # args is the sql, the page# and the rowCount
|
---|
50 | # Needs rewrite/rename
|
---|
51 | sub countRows($); # returns first element of first row of passed SQL
|
---|
52 | # Only usage passes "select count(*) ..."
|
---|
53 |
|
---|
54 | # Global variables
|
---|
55 | my $RESULTS_PER_PAGE = 50;
|
---|
56 | my %webvar = parse_post();
|
---|
57 | cleanInput(\%webvar);
|
---|
58 |
|
---|
59 | # Stuff that gets loaded from the database
|
---|
60 | #my @citylist;
|
---|
61 | #my @poplist;
|
---|
62 | #my %allocated; # Count for allocated blocks in a master block
|
---|
63 | #my %free; # Count for free blocks (routed and unrouted) in a master block
|
---|
64 | #my %bigfree; # Tracking largest free block in a master block
|
---|
65 | #my %routed; # Number of routed blocks in a master block
|
---|
66 |
|
---|
67 | # Slurp up the master block list - we need this several places
|
---|
68 | # While we're at it, initialize the related hashes.
|
---|
69 | #$sth = $ip_dbh->prepare("select * from masterblocks order by cidr");
|
---|
70 | #$sth->execute;
|
---|
71 | #for (my $i=0; my @data = $sth->fetchrow_array(); $i++) {
|
---|
72 | # $masterblocks[$i] = new NetAddr::IP $data[0];
|
---|
73 | # $allocated{"$masterblocks[$i]"} = 0;
|
---|
74 | # $free{"$masterblocks[$i]"} = 0;
|
---|
75 | # $bigfree{"$masterblocks[$i]"} = 128; # Larger number means smaller block.
|
---|
76 | # # Set to 128 to prepare for IPv6
|
---|
77 | # $routed{"$masterblocks[$i]"} = 0;
|
---|
78 | #}
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 |
|
---|
83 | #main()
|
---|
84 |
|
---|
85 | if(!defined($webvar{action})) {
|
---|
86 | $webvar{action} = "<NULL>"; #shuts up the warnings.
|
---|
87 | }
|
---|
88 |
|
---|
89 | if($webvar{action} eq 'index') {
|
---|
90 | showSummary();
|
---|
91 | } elsif ($webvar{action} eq 'newmaster') {
|
---|
92 | printHeader('');
|
---|
93 |
|
---|
94 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
95 |
|
---|
96 | print "<div type=heading align=center>Adding $cidr as master block....</div>\n";
|
---|
97 |
|
---|
98 | # Allow transactions, and raise an exception on errors so we can catch it later.
|
---|
99 | # Use local to make sure these get "reset" properly on exiting this block
|
---|
100 | local $ip_dbh->{AutoCommit} = 0;
|
---|
101 | local $ip_dbh->{RaiseError} = 1;
|
---|
102 |
|
---|
103 | # Wrap the SQL in a transaction
|
---|
104 | eval {
|
---|
105 | $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')");
|
---|
106 | $sth->execute;
|
---|
107 |
|
---|
108 | # Unrouted blocks aren't associated with a city (yet). We don't rely on this
|
---|
109 | # elsewhere though; legacy data may have traps and pitfalls in it to break this.
|
---|
110 | # Thus the "routed" flag.
|
---|
111 |
|
---|
112 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$webvar{cidr}',".
|
---|
113 | $cidr->masklen.",'<NULL>','n')");
|
---|
114 | $sth->execute;
|
---|
115 |
|
---|
116 | # If we get here, everything is happy. Commit changes.
|
---|
117 | $ip_dbh->commit;
|
---|
118 | }; # end eval
|
---|
119 |
|
---|
120 | if ($@) {
|
---|
121 | carp "Transaction aborted because $@";
|
---|
122 | eval { $ip_dbh->rollback; };
|
---|
123 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'";
|
---|
124 | printError("Could not add master block $webvar{cidr} to database: $@");
|
---|
125 | } else {
|
---|
126 | print "<div type=heading align=center>Success!</div>\n";
|
---|
127 | syslog "info", "$authuser added master block $webvar{cidr}";
|
---|
128 | }
|
---|
129 |
|
---|
130 | } # end add new master
|
---|
131 |
|
---|
132 | elsif($webvar{action} eq 'showmaster') {
|
---|
133 | showMaster();
|
---|
134 | }
|
---|
135 | elsif($webvar{action} eq 'showrouted') {
|
---|
136 | showRBlock();
|
---|
137 | }
|
---|
138 | elsif($webvar{action} eq 'listpool') {
|
---|
139 | listPool();
|
---|
140 | }
|
---|
141 | elsif($webvar{action} eq 'search') {
|
---|
142 | printHeader('');
|
---|
143 | if (!$webvar{input}) {
|
---|
144 | # No search term. Display everything.
|
---|
145 | viewBy('all', '');
|
---|
146 | } else {
|
---|
147 | # Search term entered. Display matches.
|
---|
148 | # We should really sanitize $webvar{input}, no?
|
---|
149 | viewBy($webvar{searchfor}, $webvar{input});
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | # Not modified or added; just shuffled
|
---|
154 | elsif($webvar{action} eq 'assign') {
|
---|
155 | assignBlock();
|
---|
156 | }
|
---|
157 | elsif($webvar{action} eq 'confirm') {
|
---|
158 | confirmAssign();
|
---|
159 | }
|
---|
160 | elsif($webvar{action} eq 'insert') {
|
---|
161 | insertAssign();
|
---|
162 | }
|
---|
163 | elsif($webvar{action} eq 'edit') {
|
---|
164 | edit();
|
---|
165 | }
|
---|
166 | elsif($webvar{action} eq 'update') {
|
---|
167 | update();
|
---|
168 | }
|
---|
169 | elsif($webvar{action} eq 'delete') {
|
---|
170 | remove();
|
---|
171 | }
|
---|
172 | elsif($webvar{action} eq 'finaldelete') {
|
---|
173 | finalDelete();
|
---|
174 | }
|
---|
175 |
|
---|
176 | # Default is an error. It shouldn't be possible to easily get here.
|
---|
177 | # The only way I can think of offhand is to just call main.cgi bare-
|
---|
178 | # which is not in any way guaranteed to provide anything useful.
|
---|
179 | else {
|
---|
180 | printHeader('');
|
---|
181 | my $rnd = rand 500;
|
---|
182 | my $boing = sprintf("%.2f", rand 500);
|
---|
183 | my @excuses = ("Aether cloudy. Ask again later.","The gods are unhappy with your sacrifice.",
|
---|
184 | "Because one of it's legs are both the same", "*wibble*",
|
---|
185 | "Hey! Stop pushing my buttons!", "I ain't done nuttin'", "9",
|
---|
186 | "8", "9", "10", "11", "12", "13", "14", "15", "16", "17");
|
---|
187 | printAndExit("Error $boing: ".$excuses[$rnd/30.0]);
|
---|
188 | }
|
---|
189 | ## Finally! Done with that NASTY "case" emulation!
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 | # Clean up IPDB globals, DB handle, etc.
|
---|
194 | finish($ip_dbh);
|
---|
195 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
196 | printFooter;
|
---|
197 | # Just in case something waaaayyy down isn't in place
|
---|
198 | # properly... we exit explicitly.
|
---|
199 | exit;
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 | sub viewBy($$) {
|
---|
204 | my ($category,$query) = @_;
|
---|
205 |
|
---|
206 | # Local variables
|
---|
207 | my $sql;
|
---|
208 |
|
---|
209 | #print "<pre>\n";
|
---|
210 |
|
---|
211 | #print "start querysub: query '$query'\n";
|
---|
212 | # this may happen with more than one subcategory. Unlikely, but possible.
|
---|
213 |
|
---|
214 | # Calculate start point for LIMIT clause
|
---|
215 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
216 |
|
---|
217 | # Possible cases:
|
---|
218 | # 1) Partial IP/subnet. Treated as "first-three-octets-match" in old IPDB,
|
---|
219 | # I should be able to handle it similarly here.
|
---|
220 | # 2a) CIDR subnet. Treated more or less as such in old IPDB.
|
---|
221 | # 2b) CIDR netmask. Not sure how it's treated.
|
---|
222 | # 3) Customer ID. Not handled in old IPDB
|
---|
223 | # 4) Description.
|
---|
224 | # 5) Invalid data which might be interpretable as an IP or something, but
|
---|
225 | # which probably shouldn't be for reasons of sanity.
|
---|
226 |
|
---|
227 | if ($category eq 'all') {
|
---|
228 |
|
---|
229 | print qq(<div class="heading">Showing all netblock and static-IP allocations</div><br>\n);
|
---|
230 | $sql = "select * from searchme";
|
---|
231 | my $count = countRows("select count(*) from ($sql) foo");
|
---|
232 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
233 | queryResults($sql, $webvar{page}, $count);
|
---|
234 |
|
---|
235 | } elsif ($category eq 'cust') {
|
---|
236 |
|
---|
237 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
|
---|
238 |
|
---|
239 | # Query for a customer ID. Note that we can't restrict to "numeric-only"
|
---|
240 | # as we have non-numeric custIDs in the legacy data. :/
|
---|
241 | $sql = "select * from searchme where custid ilike '%$query%'";
|
---|
242 | my $count = countRows("select count(*) from ($sql) foo");
|
---|
243 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
244 | queryResults($sql, $webvar{page}, $count);
|
---|
245 |
|
---|
246 | } elsif ($category eq 'desc') {
|
---|
247 |
|
---|
248 | print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n);
|
---|
249 | # Query based on description (includes "name" from old DB).
|
---|
250 | $sql = "select * from searchme where description ilike '%$query%'";
|
---|
251 | my $count = countRows("select count(*) from ($sql) foo");
|
---|
252 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
253 | queryResults($sql, $webvar{page}, $count);
|
---|
254 |
|
---|
255 | } elsif ($category =~ /ipblock/) {
|
---|
256 |
|
---|
257 | # Query is for a partial IP, a CIDR block in some form, or a flat IP.
|
---|
258 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
|
---|
259 |
|
---|
260 | $query =~ s/\s+//g;
|
---|
261 | if ($query =~ /\//) {
|
---|
262 | # 209.91.179/26 should show all /26 subnets in 209.91.179
|
---|
263 | my ($net,$maskbits) = split /\//, $query;
|
---|
264 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
265 | # /0->/9 are silly to worry about right now. I don't think
|
---|
266 | # we'll be getting a class A anytime soon. <g>
|
---|
267 | $sql = "select * from searchme where cidr='$query'";
|
---|
268 | queryResults($sql, $webvar{page}, 1);
|
---|
269 | } else {
|
---|
270 | print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
|
---|
271 | # Partial match; beginning of subnet and maskbits are provided
|
---|
272 | $sql = "select * from searchme where text(cidr) like '$net%' and ".
|
---|
273 | "text(cidr) like '%$maskbits'";
|
---|
274 | my $count = countRows("select count(*) from ($sql) foo");
|
---|
275 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
276 | queryResults($sql, $webvar{page}, $count);
|
---|
277 | }
|
---|
278 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
279 | # Specific IP address match
|
---|
280 | print "4-octet pattern found; finding netblock containing IP $query<br>\n";
|
---|
281 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
|
---|
282 | my $sfor = new NetAddr::IP $query;
|
---|
283 | $sth = $ip_dbh->prepare("select * from searchme where text(cidr) like '$net%'");
|
---|
284 | $sth->execute;
|
---|
285 | while (my @data = $sth->fetchrow_array()) {
|
---|
286 | my $cidr = new NetAddr::IP $data[0];
|
---|
287 | if ($cidr->contains($sfor)) {
|
---|
288 | queryResults("select * from searchme where cidr='$cidr'", $webvar{page}, 1);
|
---|
289 | }
|
---|
290 | }
|
---|
291 | } elsif ($query =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.?$/) {
|
---|
292 | print "Finding matches where the first three octets are $query<br>\n";
|
---|
293 | $sql = "select * from searchme where text(cidr) like '$query%'";
|
---|
294 | my $count = countRows("select count(*) from ($sql) foo");
|
---|
295 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset";
|
---|
296 | queryResults($sql, $webvar{page}, $count);
|
---|
297 | } else {
|
---|
298 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
299 | printError("Invalid query.");
|
---|
300 | }
|
---|
301 | } else {
|
---|
302 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
303 | printError("Invalid searchfor.");
|
---|
304 | }
|
---|
305 | } # viewBy
|
---|
306 |
|
---|
307 |
|
---|
308 | # args are: a reference to an array with the row to be printed and the
|
---|
309 | # class(stylesheet) to use for formatting.
|
---|
310 | # if ommitting the class - call the sub as &printRow(\@array)
|
---|
311 | sub printRow {
|
---|
312 | my ($rowRef,$class) = @_;
|
---|
313 |
|
---|
314 | if (!$class) {
|
---|
315 | print "<tr>\n";
|
---|
316 | } else {
|
---|
317 | print "<tr class=\"$class\">\n";
|
---|
318 | }
|
---|
319 |
|
---|
320 | ELEMENT: foreach my $element (@$rowRef) {
|
---|
321 | if (!defined($element)) {
|
---|
322 | print "<td></td>\n";
|
---|
323 | next ELEMENT;
|
---|
324 | }
|
---|
325 | $element =~ s|\n|</br>|g;
|
---|
326 | print "<td>$element</td>\n";
|
---|
327 | }
|
---|
328 | print "</tr>";
|
---|
329 | } # printRow
|
---|
330 |
|
---|
331 |
|
---|
332 | # Display certain types of search query. Note that this can't be
|
---|
333 | # cleanly reused much of anywhere else as the data isn't neatly tabulated.
|
---|
334 | # This is tied to the search sub tightly enough I may just gut it and provide
|
---|
335 | # more appropriate tables directly as needed.
|
---|
336 | sub queryResults($$$) {
|
---|
337 | my ($sql, $pageNo, $rowCount) = @_;
|
---|
338 | my $offset = 0;
|
---|
339 | $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/);
|
---|
340 |
|
---|
341 | my $sth = $ip_dbh->prepare($sql);
|
---|
342 | $sth->execute();
|
---|
343 |
|
---|
344 | startTable('Allocation','CustID','Type','City','Description/Name');
|
---|
345 | my $count = 0;
|
---|
346 |
|
---|
347 | while (my @data = $sth->fetchrow_array) {
|
---|
348 | # cidr,custid,type,city,description,notes
|
---|
349 | # Fix up types from pools (which are single-char)
|
---|
350 | # Fixing the database would be... painful. :(
|
---|
351 | if ($data[2] =~ /^[cdsmw]$/) {
|
---|
352 | $data[2] .= 'i';
|
---|
353 | }
|
---|
354 | my @row = (qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
|
---|
355 | $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]);
|
---|
356 | # Allow listing of pool if desired/required.
|
---|
357 | if ($data[2] =~ /^[cdsmw]p$/) {
|
---|
358 | $row[0] .= ' <a href="/ip/cgi-bin/main.cgi?action=listpool'.
|
---|
359 | "&pool=$data[0]\">List IPs</a>";
|
---|
360 | }
|
---|
361 | printRow(\@row, 'color1', 1) if ($count%2==0);
|
---|
362 | printRow(\@row, 'color2', 1) if ($count%2!=0);
|
---|
363 | $count++;
|
---|
364 | }
|
---|
365 |
|
---|
366 | # Have to think on this call, it's primarily to clean up unfetched rows from a select.
|
---|
367 | # In this context it's probably a good idea.
|
---|
368 | $sth->finish();
|
---|
369 |
|
---|
370 | my $upper = $offset+$count;
|
---|
371 | print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: $offset - $upper</i></td></tr>\n";
|
---|
372 | print "</table></center>\n";
|
---|
373 |
|
---|
374 | # print the page thing..
|
---|
375 | if ($rowCount > $RESULTS_PER_PAGE) {
|
---|
376 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
|
---|
377 | print qq(<div class="center"> Page: );
|
---|
378 | for (my $i = 1; $i <= $pages; $i++) {
|
---|
379 | if ($i == $pageNo) {
|
---|
380 | print "<b>$i </b>\n";
|
---|
381 | } else {
|
---|
382 | print qq(<a href="/ip/cgi-bin/main.cgi?page=$i&input=$webvar{input}&action=search&searchfor=$webvar{searchfor}">$i</a> \n);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | print "</div>";
|
---|
386 | }
|
---|
387 | } # queryResults
|
---|
388 |
|
---|
389 |
|
---|
390 | # Prints table headings. Accepts any number of arguments;
|
---|
391 | # each argument is a table heading.
|
---|
392 | sub startTable {
|
---|
393 | print qq(<center><table width="98%" cellspacing="0" class="center"><tr>);
|
---|
394 |
|
---|
395 | foreach(@_) {
|
---|
396 | print qq(<td class="heading">$_</td>);
|
---|
397 | }
|
---|
398 | print "</tr>\n";
|
---|
399 | } # startTable
|
---|
400 |
|
---|
401 |
|
---|
402 | # Return first element of passed SQL query
|
---|
403 | sub countRows($) {
|
---|
404 | my $sth = $ip_dbh->prepare($_[0]);
|
---|
405 | $sth->execute();
|
---|
406 | my @a = $sth->fetchrow_array();
|
---|
407 | $sth->finish();
|
---|
408 | return $a[0];
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | # Initial display: Show master blocks with total allocated subnets, total free subnets
|
---|
413 | sub showSummary {
|
---|
414 | # this is horrible-ugly-bad and will Go Away real soon now(TM)
|
---|
415 | print "Content-type: text/html\n\n";
|
---|
416 |
|
---|
417 | startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
|
---|
418 | 'Free netblocks', 'Largest free block');
|
---|
419 |
|
---|
420 | my %allocated;
|
---|
421 | my %free;
|
---|
422 | my %routed;
|
---|
423 | my %bigfree;
|
---|
424 |
|
---|
425 | # Count the allocations.
|
---|
426 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
|
---|
427 | foreach my $master (@masterblocks) {
|
---|
428 | $sth->execute("$master");
|
---|
429 | $sth->bind_columns(\$allocated{"$master"});
|
---|
430 | $sth->fetch();
|
---|
431 | }
|
---|
432 |
|
---|
433 | # Count routed blocks
|
---|
434 | $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?");
|
---|
435 | foreach my $master (@masterblocks) {
|
---|
436 | $sth->execute("$master");
|
---|
437 | $sth->bind_columns(\$routed{"$master"});
|
---|
438 | $sth->fetch();
|
---|
439 | }
|
---|
440 |
|
---|
441 | # Count the free blocks.
|
---|
442 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ?");
|
---|
443 | foreach my $master (@masterblocks) {
|
---|
444 | $sth->execute("$master");
|
---|
445 | $sth->bind_columns(\$free{"$master"});
|
---|
446 | $sth->fetch();
|
---|
447 | }
|
---|
448 |
|
---|
449 | # Find the largest free block in each master
|
---|
450 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? order by maskbits limit 1");
|
---|
451 | foreach my $master (@masterblocks) {
|
---|
452 | $sth->execute("$master");
|
---|
453 | $sth->bind_columns(\$bigfree{"$master"});
|
---|
454 | $sth->fetch();
|
---|
455 | }
|
---|
456 |
|
---|
457 | # Print the data.
|
---|
458 | my $count=0;
|
---|
459 | foreach my $master (@masterblocks) {
|
---|
460 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showmaster&block=$master\">$master</a>",
|
---|
461 | $routed{"$master"}, $allocated{"$master"}, $free{"$master"},
|
---|
462 | ( ($bigfree{"$master"} eq 128) ? ("<NONE>") : ("/".$bigfree{"$master"}) )
|
---|
463 | );
|
---|
464 |
|
---|
465 | printRow(\@row, 'color1' ) if($count%2==0);
|
---|
466 | printRow(\@row, 'color2' ) if($count%2!=0);
|
---|
467 | $count++;
|
---|
468 | }
|
---|
469 | print "</table>\n";
|
---|
470 | print qq(<a href="/ip/addmaster.shtml">Add new master block</a><br><br>\n);
|
---|
471 | print "Note: Free blocks noted here include both routed and unrouted blocks.\n";
|
---|
472 |
|
---|
473 | } # showSummary
|
---|
474 |
|
---|
475 |
|
---|
476 | # Display detail on master
|
---|
477 | # Alrighty then! We're showing routed blocks within a single master this time.
|
---|
478 | # We should be able to steal code from showSummary(), and if I'm really smart
|
---|
479 | # I'll figger a way to munge the two together. (Once I've done that, everything
|
---|
480 | # else should follow. YMMV.)
|
---|
481 | sub showMaster {
|
---|
482 | printHeader('');
|
---|
483 |
|
---|
484 | print qq(<center><div class="heading">Summarizing routed blocks for ).
|
---|
485 | qq($webvar{block}:</div></center><br>\n);
|
---|
486 |
|
---|
487 | my %allocated;
|
---|
488 | my %free;
|
---|
489 | my %routed;
|
---|
490 | my %bigfree;
|
---|
491 |
|
---|
492 | my $master = new NetAddr::IP $webvar{block};
|
---|
493 | my @localmasters;
|
---|
494 |
|
---|
495 | # Fetch only the blocks relevant to this master
|
---|
496 | $sth = $ip_dbh->prepare("select * from routed where cidr <<= '$master' order by cidr");
|
---|
497 | $sth->execute();
|
---|
498 |
|
---|
499 | my $i=0;
|
---|
500 | while (my @data = $sth->fetchrow_array()) {
|
---|
501 | my $cidr = new NetAddr::IP $data[0];
|
---|
502 | $localmasters[$i++] = $cidr;
|
---|
503 | $free{"$cidr"} = 0;
|
---|
504 | $allocated{"$cidr"} = 0;
|
---|
505 | $bigfree{"$cidr"} = 128;
|
---|
506 | # Retain the routing destination
|
---|
507 | $routed{"$cidr"} = $data[2];
|
---|
508 | }
|
---|
509 |
|
---|
510 | # Check if there were actually any blocks routed from this master
|
---|
511 | if ($i > 0) {
|
---|
512 | startTable('Routed block','Routed to','Allocated blocks',
|
---|
513 | 'Free blocks','Largest free block');
|
---|
514 |
|
---|
515 | # Count the allocations
|
---|
516 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?");
|
---|
517 | foreach my $master (@localmasters) {
|
---|
518 | $sth->execute("$master");
|
---|
519 | $sth->bind_columns(\$allocated{"$master"});
|
---|
520 | $sth->fetch();
|
---|
521 | }
|
---|
522 |
|
---|
523 | # Count the free blocks.
|
---|
524 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ?");
|
---|
525 | foreach my $master (@localmasters) {
|
---|
526 | $sth->execute("$master");
|
---|
527 | $sth->bind_columns(\$free{"$master"});
|
---|
528 | $sth->fetch();
|
---|
529 | }
|
---|
530 |
|
---|
531 | # Get the size of the largest free block
|
---|
532 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? order by maskbits limit 1");
|
---|
533 | foreach my $master (@localmasters) {
|
---|
534 | $sth->execute("$master");
|
---|
535 | $sth->bind_columns(\$bigfree{"$master"});
|
---|
536 | $sth->fetch();
|
---|
537 | }
|
---|
538 |
|
---|
539 | # Print the data.
|
---|
540 | my $count=0;
|
---|
541 | foreach my $master (@localmasters) {
|
---|
542 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showrouted&block=$master\">$master</a>",
|
---|
543 | $routed{"$master"}, $allocated{"$master"},
|
---|
544 | $free{"$master"},
|
---|
545 | ( ($bigfree{"$master"} eq 128) ? ("<NONE>") : ("/".$bigfree{"$master"}) )
|
---|
546 | );
|
---|
547 | printRow(\@row, 'color1' ) if($count%2==0);
|
---|
548 | printRow(\@row, 'color2' ) if($count%2!=0);
|
---|
549 | $count++;
|
---|
550 | }
|
---|
551 | } else {
|
---|
552 | # If a master block has no routed blocks, then by definition it has no
|
---|
553 | # allocations, and can be deleted.
|
---|
554 | print qq(<hr width="60%"><center><div class="heading">No allocations in ).
|
---|
555 | qq($master.</div>\n).
|
---|
556 | qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
|
---|
557 | qq(<input type=hidden name=action value="delete">\n).
|
---|
558 | qq(<input type=hidden name=block value="$master">\n).
|
---|
559 | qq(<input type=hidden name=alloctype value="mm">\n).
|
---|
560 | qq(<input type=submit value=" Remove this master ">\n).
|
---|
561 | qq(</form></center>\n);
|
---|
562 |
|
---|
563 | } # end check for existence of routed blocks in master
|
---|
564 |
|
---|
565 | print qq(</table>\n<hr width="60%">\n).
|
---|
566 | qq(<center><div class="heading">Unrouted blocks in $master:</div></center><br>\n);
|
---|
567 |
|
---|
568 | startTable('Netblock','Range');
|
---|
569 |
|
---|
570 | # Snag the free blocks.
|
---|
571 | my $count = 0;
|
---|
572 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr <<='$master' and ".
|
---|
573 | "routed='n' order by cidr");
|
---|
574 | $sth->execute();
|
---|
575 | while (my @data = $sth->fetchrow_array()) {
|
---|
576 | my $cidr = new NetAddr::IP $data[0];
|
---|
577 | my @row = ("$cidr", $cidr->range);
|
---|
578 | printRow(\@row, 'color1' ) if($count%2==0);
|
---|
579 | printRow(\@row, 'color2' ) if($count%2!=0);
|
---|
580 | $count++;
|
---|
581 | }
|
---|
582 |
|
---|
583 | print "</table>\n";
|
---|
584 | } # showMaster
|
---|
585 |
|
---|
586 |
|
---|
587 | # Display details of a routed block
|
---|
588 | # Alrighty then! We're showing allocations within a routed block this time.
|
---|
589 | # We should be able to steal code from showSummary() and showMaster(), and if
|
---|
590 | # I'm really smart I'll figger a way to munge all three together. (Once I've
|
---|
591 | # done that, everything else should follow. YMMV.
|
---|
592 | # This time, we check the database before spewing, because we may
|
---|
593 | # not have anything useful to spew.
|
---|
594 | sub showRBlock {
|
---|
595 | printHeader('');
|
---|
596 |
|
---|
597 | my $master = new NetAddr::IP $webvar{block};
|
---|
598 |
|
---|
599 | $sth = $ip_dbh->prepare("select * from routed where cidr='$master'");
|
---|
600 | $sth->execute;
|
---|
601 | my @data = $sth->fetchrow_array;
|
---|
602 |
|
---|
603 | print qq(<center><div class="heading">Summarizing allocated blocks for ).
|
---|
604 | qq($master ($data[2]):</div></center><br>\n);
|
---|
605 |
|
---|
606 | startTable('CIDR allocation','Customer Location','Type','CustID','Description/Name');
|
---|
607 |
|
---|
608 | # Snag the allocations for this block
|
---|
609 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$master' order by cidr");
|
---|
610 | $sth->execute();
|
---|
611 |
|
---|
612 | my $count=0;
|
---|
613 | while (my @data = $sth->fetchrow_array()) {
|
---|
614 | # cidr,custid,type,city,description,notes,maskbits,circuitid
|
---|
615 | my $cidr = new NetAddr::IP $data[0];
|
---|
616 |
|
---|
617 | # Clean up extra spaces that are borking things.
|
---|
618 | $data[2] =~ s/\s+//g;
|
---|
619 |
|
---|
620 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=edit&block=$data[0]\">$data[0]</a>",
|
---|
621 | $data[3], $disp_alloctypes{$data[2]}, $data[1], $data[4]);
|
---|
622 | # If the allocation is a pool, allow listing of the IPs in the pool.
|
---|
623 | if ($data[2] =~ /^[cdsmw]p$/) {
|
---|
624 | $row[0] .= ' <a href="/ip/cgi-bin/main.cgi?action=listpool'.
|
---|
625 | "&pool=$data[0]\">List IPs</a>";
|
---|
626 | }
|
---|
627 |
|
---|
628 | printRow(\@row, 'color1') if ($count%2 == 0);
|
---|
629 | printRow(\@row, 'color2') if ($count%2 != 0);
|
---|
630 | $count++;
|
---|
631 | }
|
---|
632 |
|
---|
633 | print "</table>\n";
|
---|
634 |
|
---|
635 | # If the routed block has no allocations, by definition it only has
|
---|
636 | # one free block, and therefore may be deleted.
|
---|
637 | if ($count == 0) {
|
---|
638 | print qq(<hr width="60%"><center><div class="heading">No allocations in ).
|
---|
639 | qq($master.</div></center>\n).
|
---|
640 | qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n).
|
---|
641 | qq(<input type=hidden name=action value="delete">\n).
|
---|
642 | qq(<input type=hidden name=block value="$master">\n).
|
---|
643 | qq(<input type=hidden name=alloctype value="rr">\n).
|
---|
644 | qq(<input type=submit value=" Remove this block ">\n).
|
---|
645 | qq(</form>\n);
|
---|
646 | }
|
---|
647 |
|
---|
648 | print qq(<hr width="60%">\n<center><div class="heading">Free blocks within routed ).
|
---|
649 | qq(submaster $master</div></center>\n);
|
---|
650 |
|
---|
651 | startTable('CIDR block','Range');
|
---|
652 |
|
---|
653 | # Snag the free blocks. We don't really *need* to be pedantic about avoiding
|
---|
654 | # unrouted free blocks, but it's better to let the database do the work if we can.
|
---|
655 | $count = 0;
|
---|
656 | $sth = $ip_dbh->prepare("select * from freeblocks where routed='y' and cidr <<= '$master' order by cidr");
|
---|
657 | $sth->execute();
|
---|
658 | while (my @data = $sth->fetchrow_array()) {
|
---|
659 | # cidr,maskbits,city
|
---|
660 | my $cidr = new NetAddr::IP $data[0];
|
---|
661 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=assign&block=$cidr\">$cidr</a>",
|
---|
662 | $cidr->range);
|
---|
663 | printRow(\@row, 'color1') if ($count%2 == 0);
|
---|
664 | printRow(\@row, 'color2') if ($count%2 != 0);
|
---|
665 | $count++;
|
---|
666 | }
|
---|
667 |
|
---|
668 | print "</table>\n";
|
---|
669 | } # showRBlock
|
---|
670 |
|
---|
671 |
|
---|
672 | # List the IPs used in a pool
|
---|
673 | sub listPool {
|
---|
674 | printHeader('');
|
---|
675 |
|
---|
676 | my $cidr = new NetAddr::IP $webvar{pool};
|
---|
677 |
|
---|
678 | # Snag pool info for heading
|
---|
679 | $sth = $ip_dbh->prepare("select * from allocations where cidr='$cidr'");
|
---|
680 | $sth->execute;
|
---|
681 | my @data = $sth->fetchrow_array;
|
---|
682 | my $type = $data[2]; # We'll need this later.
|
---|
683 |
|
---|
684 | print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
|
---|
685 | qq(($disp_alloctypes{$type} in $data[3])</div></center><br>\n);
|
---|
686 | print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
|
---|
687 | print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
|
---|
688 | $cidr->addr."</td></tr>\n";
|
---|
689 | $cidr++;
|
---|
690 | print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
|
---|
691 | $cidr--; $cidr--;
|
---|
692 | print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
|
---|
693 | "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
|
---|
694 | "</table></div></div>\n";
|
---|
695 |
|
---|
696 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
697 |
|
---|
698 | startTable('IP','Customer ID','Available?','Description','');
|
---|
699 | $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{pool}' order by ip");
|
---|
700 | $sth->execute;
|
---|
701 | my $count = 0;
|
---|
702 | while (my @data = $sth->fetchrow_array) {
|
---|
703 | # pool,ip,custid,city,ptype,available,notes,description,circuitid
|
---|
704 | # If desc is null, make it not null. <g>
|
---|
705 | if ($data[7] eq '') {
|
---|
706 | $data[7] = ' ';
|
---|
707 | }
|
---|
708 | # Some nice hairy Perl to decide whether to allow unassigning each IP
|
---|
709 | # -> if $data[5] (aka poolips.available) == 'n' then we print the unassign link
|
---|
710 | # else we print a blank space
|
---|
711 | my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[1]">$data[1]</a>),
|
---|
712 | $data[2],$data[5],$data[7],
|
---|
713 | ( ($data[5] eq 'n') ?
|
---|
714 | ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[1]&".
|
---|
715 | "alloctype=$data[4]i\">Unassign this IP</a>") :
|
---|
716 | (" ") )
|
---|
717 | );
|
---|
718 | printRow(\@row, 'color1') if($count%2==0);
|
---|
719 | printRow(\@row, 'color2') if($count%2!=0);
|
---|
720 | $count++;
|
---|
721 | }
|
---|
722 | print "</table>\n";
|
---|
723 |
|
---|
724 | } # end listPool
|
---|
725 |
|
---|
726 |
|
---|
727 | # Show "Add new allocation" page. Note that the actual page may
|
---|
728 | # be one of two templates, and the lists come from the database.
|
---|
729 | sub assignBlock {
|
---|
730 | printHeader('');
|
---|
731 |
|
---|
732 | my $html;
|
---|
733 |
|
---|
734 | # New special case- block to assign is specified
|
---|
735 | if ($webvar{block} ne '') {
|
---|
736 | open HTML, "../fb-assign.html"
|
---|
737 | or croak "Could not open fb-assign.html: $!";
|
---|
738 | $html = join('',<HTML>);
|
---|
739 | close HTML;
|
---|
740 | my $block = new NetAddr::IP $webvar{block};
|
---|
741 | $html =~ s|\$\$BLOCK\$\$|$block|g;
|
---|
742 | $html =~ s|\$\$MASKBITS\$\$|$block->masklen|;
|
---|
743 | my $typelist = '';
|
---|
744 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 and type not like '_i' order by listorder");
|
---|
745 | $sth->execute;
|
---|
746 | my @data = $sth->fetchrow_array;
|
---|
747 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
|
---|
748 | while (my @data = $sth->fetchrow_array) {
|
---|
749 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
|
---|
750 | }
|
---|
751 | $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
|
---|
752 | } else {
|
---|
753 | open HTML, "../assign.html"
|
---|
754 | or croak "Could not open assign.html: $!";
|
---|
755 | $html = join('',<HTML>);
|
---|
756 | close HTML;
|
---|
757 | my $masterlist = "<select name=allocfrom><option selected>-</option>\n";
|
---|
758 | foreach my $master (@masterblocks) {
|
---|
759 | $masterlist .= "<option>$master</option>\n";
|
---|
760 | }
|
---|
761 | $masterlist .= "</select>\n";
|
---|
762 | $html =~ s|\$\$MASTERLIST\$\$|$masterlist|g;
|
---|
763 | my $pops = '';
|
---|
764 | foreach my $pop (@poplist) {
|
---|
765 | $pops .= "<option>$pop</option>\n";
|
---|
766 | }
|
---|
767 | $html =~ s|\$\$POPLIST\$\$|$pops|g;
|
---|
768 | my $typelist = '';
|
---|
769 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
|
---|
770 | $sth->execute;
|
---|
771 | my @data = $sth->fetchrow_array;
|
---|
772 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
|
---|
773 | while (my @data = $sth->fetchrow_array) {
|
---|
774 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
|
---|
775 | }
|
---|
776 | $html =~ s|\$\$TYPELIST\$\$|$typelist|g;
|
---|
777 | }
|
---|
778 | my $cities = '';
|
---|
779 | foreach my $city (@citylist) {
|
---|
780 | $cities .= "<option>$city</option>\n";
|
---|
781 | }
|
---|
782 | $html =~ s|\$\$ALLCITIES\$\$|$cities|g;
|
---|
783 |
|
---|
784 | print $html;
|
---|
785 |
|
---|
786 | } # assignBlock
|
---|
787 |
|
---|
788 |
|
---|
789 | # Take info on requested IP assignment and see what we can provide.
|
---|
790 | sub confirmAssign {
|
---|
791 | printHeader('');
|
---|
792 |
|
---|
793 | my $cidr;
|
---|
794 | my $alloc_from;
|
---|
795 |
|
---|
796 | # Going to manually validate some items.
|
---|
797 | # custid and city are automagic.
|
---|
798 | return if !validateInput();
|
---|
799 |
|
---|
800 | # Several different cases here.
|
---|
801 | # Static IP vs netblock
|
---|
802 | # + Different flavours of static IP
|
---|
803 | # + Different flavours of netblock
|
---|
804 |
|
---|
805 | if ($webvar{alloctype} =~ /^[cdsmw]i$/) {
|
---|
806 | my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
|
---|
807 | my $sql;
|
---|
808 | # Check for pools in Subury or North Bay if DSL or server pool. Anywhere else is
|
---|
809 | # invalid and shouldn't be in the db in the first place.
|
---|
810 | # ... aside from #^%#$%#@#^%^^!!!! legacy data. GRRR.
|
---|
811 | # Note that we want to retain the requested city to relate to customer info.
|
---|
812 | if ($base =~ /^[ds]$/) {
|
---|
813 | $sql = "select * from poolips where available='y' and".
|
---|
814 | " ptype='$base' and (city='Sudbury' or city='North Bay')";
|
---|
815 | } else {
|
---|
816 | $sql = "select * from poolips where available='y' and".
|
---|
817 | " ptype='$base' and city='$webvar{pop}'";
|
---|
818 | }
|
---|
819 |
|
---|
820 | # Now that we know where we're looking, we can list the pools with free IPs.
|
---|
821 | $sth = $ip_dbh->prepare($sql);
|
---|
822 | $sth->execute;
|
---|
823 | my %ipcount;
|
---|
824 | my $optionlist;
|
---|
825 | while (my @data = $sth->fetchrow_array) {
|
---|
826 | $ipcount{$data[0]}++;
|
---|
827 | }
|
---|
828 | $sth = $ip_dbh->prepare("select city from allocations where cidr=?");
|
---|
829 | foreach my $key (keys %ipcount) {
|
---|
830 | $sth->execute($key);
|
---|
831 | my @data = $sth->fetchrow_array;
|
---|
832 | $optionlist .= "<option value='$key'>$key [$ipcount{$key} free IP(s)] in $data[0]</option>\n";
|
---|
833 | }
|
---|
834 | $cidr = "Single static IP";
|
---|
835 | $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
|
---|
836 |
|
---|
837 | } else { # end show pool options
|
---|
838 |
|
---|
839 | if ($webvar{fbassign} eq 'y') {
|
---|
840 | $cidr = new NetAddr::IP $webvar{block};
|
---|
841 | $webvar{maskbits} = $cidr->masklen;
|
---|
842 | } else { # done with direct freeblocks assignment
|
---|
843 |
|
---|
844 | if (!$webvar{maskbits}) {
|
---|
845 | printError("Please specify a CIDR mask length.");
|
---|
846 | return;
|
---|
847 | }
|
---|
848 | my $sql;
|
---|
849 | my $city;
|
---|
850 | my $failmsg;
|
---|
851 | if ($webvar{alloctype} eq 'rr') {
|
---|
852 | if ($webvar{allocfrom} ne '-') {
|
---|
853 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
|
---|
854 | " and cidr <<= '$webvar{allocfrom}' order by maskbits desc";
|
---|
855 | } else {
|
---|
856 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
|
---|
857 | " order by maskbits desc";
|
---|
858 | }
|
---|
859 | $failmsg = "No suitable free block found.<br>\nWe do not have a free".
|
---|
860 | " routeable block of that size.<br>\nYou will have to either route".
|
---|
861 | " a set of smaller netblocks or a single smaller netblock.";
|
---|
862 | } else {
|
---|
863 | ##fixme
|
---|
864 | # This section needs serious Pondering.
|
---|
865 | if ($webvar{alloctype} =~ /^[cdsmw]p$/) {
|
---|
866 | if (($webvar{city} !~ /^(Sudbury|North Bay)$/) && ($webvar{alloctype} eq 'dp')) {
|
---|
867 | printError("You must chose Sudbury or North Bay for DSL pools.");
|
---|
868 | return;
|
---|
869 | }
|
---|
870 | $city = $webvar{city};
|
---|
871 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
872 | " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller".
|
---|
873 | " block size for the pool.";
|
---|
874 | } else {
|
---|
875 | $city = $webvar{pop};
|
---|
876 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
877 | " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or".
|
---|
878 | " chose a smaller blocksize.";
|
---|
879 | }
|
---|
880 | if ($webvar{allocfrom} ne '-') {
|
---|
881 | $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
|
---|
882 | " and cidr <<= '$webvar{allocfrom}' and routed='y' order by cidr,maskbits desc";
|
---|
883 | } else {
|
---|
884 | $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
|
---|
885 | " and routed='y' order by cidr,maskbits desc";
|
---|
886 | }
|
---|
887 | }
|
---|
888 | $sth = $ip_dbh->prepare($sql);
|
---|
889 | $sth->execute;
|
---|
890 | my @data = $sth->fetchrow_array();
|
---|
891 | if ($data[0] eq "") {
|
---|
892 | printError($failmsg);
|
---|
893 | return;
|
---|
894 | }
|
---|
895 | $cidr = new NetAddr::IP $data[0];
|
---|
896 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
897 |
|
---|
898 | $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">);
|
---|
899 |
|
---|
900 | # If the block to be allocated is smaller than the one we found,
|
---|
901 | # figure out the "real" block to be allocated.
|
---|
902 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
903 | my $maskbits = $cidr->masklen();
|
---|
904 | my @subblocks;
|
---|
905 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
906 | @subblocks = $cidr->split($maskbits);
|
---|
907 | }
|
---|
908 | $cidr = $subblocks[0];
|
---|
909 | }
|
---|
910 | } # if ($webvar{alloctype} =~ /^[cdsmw]i$/)
|
---|
911 |
|
---|
912 | open HTML, "../confirm.html"
|
---|
913 | or croak "Could not open confirm.html: $!";
|
---|
914 | my $html = join '', <HTML>;
|
---|
915 | close HTML;
|
---|
916 |
|
---|
917 | ### gotta fix this in final
|
---|
918 | # Stick in customer info as necessary - if it's blank, it just ends
|
---|
919 | # up as blank lines ignored in the rendering of the page
|
---|
920 | my $custbits;
|
---|
921 | $html =~ s|\$\$CUSTBITS\$\$|$custbits|g;
|
---|
922 | ###
|
---|
923 |
|
---|
924 | # Stick in the allocation data
|
---|
925 | $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g;
|
---|
926 | $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g;
|
---|
927 | $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
|
---|
928 | $html =~ s|\$\$CIDR\$\$|$cidr|g;
|
---|
929 | $webvar{city} = desanitize($webvar{city});
|
---|
930 | $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
|
---|
931 | $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
|
---|
932 | $webvar{circid} = desanitize($webvar{circid});
|
---|
933 | $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g;
|
---|
934 | $webvar{desc} = desanitize($webvar{desc});
|
---|
935 | $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
|
---|
936 | $webvar{notes} = desanitize($webvar{notes});
|
---|
937 | $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
|
---|
938 | $html =~ s|\$\$ACTION\$\$|insert|g;
|
---|
939 |
|
---|
940 | print $html;
|
---|
941 |
|
---|
942 | } # end confirmAssign
|
---|
943 |
|
---|
944 |
|
---|
945 | # Do the work of actually inserting a block in the database.
|
---|
946 | sub insertAssign {
|
---|
947 | # Some things are done more than once.
|
---|
948 | printHeader('');
|
---|
949 | return if !validateInput();
|
---|
950 |
|
---|
951 | # $code is "success" vs "failure", $msg contains OK for a
|
---|
952 | # successful netblock allocation, the IP allocated for static
|
---|
953 | # IP, or the error message if an error occurred.
|
---|
954 | my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from},
|
---|
955 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
956 | $webvar{circid});
|
---|
957 |
|
---|
958 | if ($code eq 'OK') {
|
---|
959 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
960 | print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div></div>);
|
---|
961 | # Notify tech@example.com
|
---|
962 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
963 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
964 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
965 | } else {
|
---|
966 | print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ).
|
---|
967 | "sucessfully added as type '$webvar{alloctype}' ".
|
---|
968 | "($disp_alloctypes{$webvar{alloctype}})</div></div>";
|
---|
969 | }
|
---|
970 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
971 | "'$webvar{alloctype}'";
|
---|
972 | } else {
|
---|
973 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
974 | "'$webvar{alloctype}' by $authuser failed: '$msg'";
|
---|
975 | printError("Allocation of $webvar{fullcidr} as $disp_alloctypes{$webvar{alloctype}}".
|
---|
976 | " failed: $msg\n");
|
---|
977 | }
|
---|
978 |
|
---|
979 | } # end insertAssign()
|
---|
980 |
|
---|
981 |
|
---|
982 | # Does some basic checks on common input data to make sure nothing
|
---|
983 | # *really* weird gets in to the database through this script.
|
---|
984 | # Does NOT do complete input validation!!!
|
---|
985 | sub validateInput {
|
---|
986 | if ($webvar{city} eq '-') {
|
---|
987 | printError("Please choose a city.");
|
---|
988 | return;
|
---|
989 | }
|
---|
990 | chomp $webvar{alloctype};
|
---|
991 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
992 | if ($webvar{alloctype} =~ /^(ci|di|cn|mi|wi)$/) {
|
---|
993 | if (!$webvar{custid}) {
|
---|
994 | printError("Please enter a customer ID.");
|
---|
995 | return;
|
---|
996 | }
|
---|
997 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
998 | # Force uppercase for now...
|
---|
999 | $webvar{custid} =~ tr/a-z/A-Z/;
|
---|
1000 | # Crosscheck with ... er... something.
|
---|
1001 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
1002 | if ($CustIDCK::Error) {
|
---|
1003 | printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
1004 | return;
|
---|
1005 | }
|
---|
1006 | if (!$status) {
|
---|
1007 | printError("Customer ID not valid. Make sure the Customer ID ".
|
---|
1008 | "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
|
---|
1009 | "non-customer assignments.");
|
---|
1010 | return;
|
---|
1011 | }
|
---|
1012 | #"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for
|
---|
1013 | #static IPs for staff.");
|
---|
1014 | }
|
---|
1015 | # print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
1016 | } elsif ($webvar{alloctype} =~ /^([cdsmw]p|si|dn|dy|dc|dw|ee|rr|in)$/){
|
---|
1017 | # All non-customer allocations MUST be entered with "our" customer ID.
|
---|
1018 | # I have Defined this as 6750400 for consistency.
|
---|
1019 | # STAFF is also acceptable.
|
---|
1020 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) {
|
---|
1021 | $webvar{custid} = "6750400";
|
---|
1022 | }
|
---|
1023 | } else {
|
---|
1024 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
1025 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
1026 | printError("Invalid alloctype");
|
---|
1027 | return;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | # Check POP location
|
---|
1031 | my $flag;
|
---|
1032 | if ($webvar{alloctype} eq 'rr') {
|
---|
1033 | $flag = 'for a routed netblock';
|
---|
1034 | foreach (@poplist) {
|
---|
1035 | if (/^$webvar{city}$/) {
|
---|
1036 | $flag = 'n';
|
---|
1037 | last;
|
---|
1038 | }
|
---|
1039 | }
|
---|
1040 | } else {
|
---|
1041 | $flag = 'n';
|
---|
1042 | if ($webvar{pop} =~ /^-$/) {
|
---|
1043 | $flag = 'to route the block from/through';
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 | if ($flag ne 'n') {
|
---|
1047 | printError("Please choose a valid POP location $flag. Valid ".
|
---|
1048 | "POP locations are currently:<br>\n".join (" - ", @poplist));
|
---|
1049 | return;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | return 'OK';
|
---|
1053 | } # end validateInput
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | # Displays details of a specific allocation in a form
|
---|
1057 | # Allows update/delete
|
---|
1058 | # action=edit
|
---|
1059 | sub edit {
|
---|
1060 | printHeader('');
|
---|
1061 |
|
---|
1062 | my $sql;
|
---|
1063 |
|
---|
1064 | # Two cases: block is a netblock, or block is a static IP from a pool
|
---|
1065 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
|
---|
1066 | if ($webvar{block} =~ /\/32$/) {
|
---|
1067 | $sql = "select ip,custid,ptype,city,circuitid,description,notes from poolips where ip='$webvar{block}'";
|
---|
1068 | } else {
|
---|
1069 | $sql = "select cidr,custid,type,city,circuitid,description,notes from allocations where cidr='$webvar{block}'"
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | # gotta snag block info from db
|
---|
1073 | $sth = $ip_dbh->prepare($sql);
|
---|
1074 | $sth->execute;
|
---|
1075 | my @data = $sth->fetchrow_array;
|
---|
1076 |
|
---|
1077 | # Clean up extra whitespace on alloc type
|
---|
1078 | $data[2] =~ s/\s//;
|
---|
1079 |
|
---|
1080 | # Postfix "i" on pool IP types
|
---|
1081 | if ($data[2] =~ /^[cdsmw]$/) {
|
---|
1082 | $data[2] .= "i";
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | open (HTML, "../editDisplay.html")
|
---|
1086 | or croak "Could not open editDisplay.html :$!";
|
---|
1087 | my $html = join('', <HTML>);
|
---|
1088 |
|
---|
1089 | # We can't let the city be changed here; this block is a part of
|
---|
1090 | # a larger routed allocation and therefore by definition can't be moved.
|
---|
1091 | # block and city are static.
|
---|
1092 | ##fixme
|
---|
1093 | # Needs thinking. Have to allow changes to city to correct errors, no?
|
---|
1094 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
1095 | $html =~ s/\$\$CITY\$\$/$data[3]/g;
|
---|
1096 |
|
---|
1097 | # Screw it. Changing allocation types gets very ugly VERY quickly- especially
|
---|
1098 | # with the much longer list of allocation types.
|
---|
1099 | # We'll just show what type of block it is.
|
---|
1100 |
|
---|
1101 | # this has now been Requested, so here goes.
|
---|
1102 |
|
---|
1103 | if ($data[2] =~ /^d[nyc]|cn|ee|in$/) {
|
---|
1104 | # Block that can be changed
|
---|
1105 | my $blockoptions = "<select name=alloctype><option".
|
---|
1106 | (($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
|
---|
1107 | (($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
|
---|
1108 | (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
|
---|
1109 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
|
---|
1110 | (($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
|
---|
1111 | (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
|
---|
1112 | "</select>\n";
|
---|
1113 | $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g;
|
---|
1114 | } else {
|
---|
1115 | $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g;
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | # These can be modified, although CustID changes may get ignored.
|
---|
1119 | $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
|
---|
1120 | $html =~ s/\$\$TYPE\$\$/$data[2]/g;
|
---|
1121 | $html =~ s/\$\$CIRCID\$\$/$data[4]/g;
|
---|
1122 | $html =~ s/\$\$DESC\$\$/$data[5]/g;
|
---|
1123 | $html =~ s/\$\$NOTES\$\$/$data[6]/g;
|
---|
1124 |
|
---|
1125 | print $html;
|
---|
1126 |
|
---|
1127 | } # edit()
|
---|
1128 |
|
---|
1129 |
|
---|
1130 | # Stuff new info about a block into the db
|
---|
1131 | # action=update
|
---|
1132 | sub update {
|
---|
1133 | printHeader('');
|
---|
1134 |
|
---|
1135 | # Make sure incoming data is in correct format - custID among other things.
|
---|
1136 | validateInput;
|
---|
1137 |
|
---|
1138 | # SQL transaction wrapper
|
---|
1139 | eval {
|
---|
1140 | # Relatively simple SQL transaction here.
|
---|
1141 | my $sql;
|
---|
1142 | if (my $pooltype = ($webvar{alloctype} =~ /^([cdsmw])i$/) ) {
|
---|
1143 | $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',".
|
---|
1144 | "circuitid='$webvar{circid}',description='$webvar{desc}' ".
|
---|
1145 | "where ip='$webvar{block}'";
|
---|
1146 | } else {
|
---|
1147 | $sql = "update allocations set custid='$webvar{custid}',".
|
---|
1148 | "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',".
|
---|
1149 | "type='$webvar{alloctype}',circuitid='$webvar{circid}' where cidr='$webvar{block}'";
|
---|
1150 | }
|
---|
1151 | syslog "debug", $sql;
|
---|
1152 | $sth = $ip_dbh->prepare($sql);
|
---|
1153 | $sth->execute;
|
---|
1154 | $ip_dbh->commit;
|
---|
1155 | };
|
---|
1156 | if ($@) {
|
---|
1157 | carp "Transaction aborted because $@";
|
---|
1158 | eval { $ip_dbh->rollback; };
|
---|
1159 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
|
---|
1160 | printError("Could not update block/IP $webvar{block}: $@");
|
---|
1161 | return;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | # If we get here, the operation succeeded.
|
---|
1165 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
1166 | open (HTML, "../updated.html")
|
---|
1167 | or croak "Could not open updated.html :$!";
|
---|
1168 | my $html = join('', <HTML>);
|
---|
1169 |
|
---|
1170 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
1171 | $webvar{city} = desanitize($webvar{city});
|
---|
1172 | $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
|
---|
1173 | $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
|
---|
1174 | $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g;
|
---|
1175 | $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
|
---|
1176 | $webvar{circid} = desanitize($webvar{circid});
|
---|
1177 | $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g;
|
---|
1178 | $webvar{desc} = desanitize($webvar{desc});
|
---|
1179 | $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
|
---|
1180 | $webvar{notes} = desanitize($webvar{notes});
|
---|
1181 | $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
|
---|
1182 |
|
---|
1183 | print $html;
|
---|
1184 |
|
---|
1185 | } # update()
|
---|
1186 |
|
---|
1187 |
|
---|
1188 | # Delete an allocation.
|
---|
1189 | sub remove {
|
---|
1190 | printHeader('');
|
---|
1191 | #show confirm screen.
|
---|
1192 | open HTML, "../confirmRemove.html"
|
---|
1193 | or croak "Could not open confirmRemove.html :$!";
|
---|
1194 | my $html = join('', <HTML>);
|
---|
1195 | close HTML;
|
---|
1196 |
|
---|
1197 | # Serves'em right for getting here...
|
---|
1198 | if (!defined($webvar{block})) {
|
---|
1199 | printError("Error 332");
|
---|
1200 | return;
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype);
|
---|
1204 |
|
---|
1205 | if ($webvar{alloctype} eq 'rr') {
|
---|
1206 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
|
---|
1207 | $sth->execute();
|
---|
1208 |
|
---|
1209 | # This feels... extreme.
|
---|
1210 | croak $sth->errstr() if($sth->errstr());
|
---|
1211 |
|
---|
1212 | $sth->bind_columns(\$cidr,\$city);
|
---|
1213 | $sth->execute();
|
---|
1214 | $sth->fetch || croak $sth->errstr();
|
---|
1215 | $custid = "N/A";
|
---|
1216 | $alloctype = $webvar{alloctype};
|
---|
1217 | $circid = "N/A";
|
---|
1218 | $desc = "N/A";
|
---|
1219 | $notes = "N/A";
|
---|
1220 |
|
---|
1221 | } elsif ($webvar{alloctype} eq 'mm') {
|
---|
1222 | $cidr = $webvar{block};
|
---|
1223 | $city = "N/A";
|
---|
1224 | $custid = "N/A";
|
---|
1225 | $alloctype = $webvar{alloctype};
|
---|
1226 | $circid = "N/A";
|
---|
1227 | $desc = "N/A";
|
---|
1228 | $notes = "N/A";
|
---|
1229 | } elsif ($webvar{alloctype} =~ /^[cdsmw]i$/) { # done with alloctype=rr
|
---|
1230 |
|
---|
1231 | # Unassigning a static IP
|
---|
1232 | my $sth = $ip_dbh->prepare("select ip,custid,city,ptype,notes,circuitid from poolips".
|
---|
1233 | " where ip='$webvar{block}'");
|
---|
1234 | $sth->execute();
|
---|
1235 | # croak $sth->errstr() if($sth->errstr());
|
---|
1236 |
|
---|
1237 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid);
|
---|
1238 | $sth->fetch() || croak $sth->errstr;
|
---|
1239 |
|
---|
1240 | $alloctype .="i";
|
---|
1241 |
|
---|
1242 | } else { # done with alloctype=[cdsmw]i
|
---|
1243 |
|
---|
1244 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes from ".
|
---|
1245 | "allocations where cidr='$webvar{block}'");
|
---|
1246 | $sth->execute();
|
---|
1247 | # croak $sth->errstr() if($sth->errstr());
|
---|
1248 |
|
---|
1249 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc, \$notes);
|
---|
1250 | $sth->fetch() || carp $sth->errstr;
|
---|
1251 | } # end cases for different alloctypes
|
---|
1252 |
|
---|
1253 | # Munge everything into HTML
|
---|
1254 | $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
|
---|
1255 | $html =~ s|\$\$BLOCK\$\$|$cidr|g;
|
---|
1256 | $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g;
|
---|
1257 | $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
|
---|
1258 | $html =~ s|\$\$CITY\$\$|$city|g;
|
---|
1259 | $html =~ s|\$\$CUSTID\$\$|$custid|g;
|
---|
1260 | $html =~ s|\$\$CIRCID\$\$|$circid|g;
|
---|
1261 | $html =~ s|\$\$DESC\$\$|$desc|g;
|
---|
1262 | $html =~ s|\$\$NOTES\$\$|$notes|g;
|
---|
1263 |
|
---|
1264 | $html =~ s|\$\$ACTION\$\$|finaldelete|g;
|
---|
1265 |
|
---|
1266 | # Set the warning text.
|
---|
1267 | if ($alloctype =~ /^[cdsmw]p$/) {
|
---|
1268 | $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.<br>Any IPs allocated from this pool will also be removed!</div></td></tr>|;
|
---|
1269 | } else {
|
---|
1270 | $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | print $html;
|
---|
1274 | } # end edit()
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
1278 | # data as necessary to keep as few records as possible in freeblocks
|
---|
1279 | # to prevent weirdness when allocating blocks later.
|
---|
1280 | # Remove IPs from pool listing if necessary
|
---|
1281 | sub finalDelete {
|
---|
1282 | printHeader('');
|
---|
1283 |
|
---|
1284 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
|
---|
1285 |
|
---|
1286 | if ($code eq 'OK') {
|
---|
1287 | print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
|
---|
1288 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}";
|
---|
1289 | } else {
|
---|
1290 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
1291 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'";
|
---|
1292 | printError("Could not deallocate static IP $webvar{block}: $msg");
|
---|
1293 | } else {
|
---|
1294 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'";
|
---|
1295 | printError("Could not deallocate netblock $webvar{block}: $msg");
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | } # finalDelete
|
---|
1300 |
|
---|
1301 |
|
---|
1302 | # Just in case we manage to get here.
|
---|
1303 | exit 0;
|
---|