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