[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; };
|
---|
[17] | 140 | syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'";
|
---|
[4] | 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)) {
|
---|
[21] | 696 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=assign&block=$cidr\">$cidr</a>",
|
---|
| 697 | $cidr->range);
|
---|
[4] | 698 | printRow(\@row, 'color1') if ($count%2 == 0);
|
---|
| 699 | printRow(\@row, 'color2') if ($count%2 != 0);
|
---|
| 700 | $count++;
|
---|
| 701 | }
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | print "</table>\n";
|
---|
| 705 | printFooter;
|
---|
| 706 | } # showRBlock
|
---|
| 707 |
|
---|
| 708 |
|
---|
| 709 | # List the IPs used in a pool
|
---|
| 710 | sub listPool {
|
---|
| 711 | printHeader('');
|
---|
| 712 |
|
---|
| 713 | my $cidr = new NetAddr::IP $webvar{pool};
|
---|
| 714 |
|
---|
| 715 | # Snag pool info for heading
|
---|
| 716 | $sth = $ip_dbh->prepare("select * from allocations where cidr='$cidr'");
|
---|
| 717 | $sth->execute;
|
---|
| 718 | my @data = $sth->fetchrow_array;
|
---|
| 719 | my $type = $data[2]; # We'll need this later.
|
---|
| 720 |
|
---|
| 721 | print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n).
|
---|
| 722 | qq(($full_alloc_types{$type} in $data[3])</div></center><br>\n);
|
---|
| 723 | print qq(<div class="indent"><b>Reserved IPs:</b><br>\n);
|
---|
| 724 | print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>).
|
---|
| 725 | $cidr->addr."</td></tr>\n";
|
---|
| 726 | $cidr++;
|
---|
| 727 | print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n";
|
---|
| 728 | $cidr--; $cidr--;
|
---|
| 729 | print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n".
|
---|
| 730 | "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n".
|
---|
| 731 | "</table></div></div>\n";
|
---|
| 732 |
|
---|
| 733 | # probably have to add an "edit IP allocation" link here somewhere.
|
---|
| 734 |
|
---|
| 735 | startTable('IP','Customer ID','Available?','Description','');
|
---|
| 736 | $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{pool}' order by ip");
|
---|
| 737 | $sth->execute;
|
---|
| 738 | my $count = 0;
|
---|
| 739 | while (my @data = $sth->fetchrow_array) {
|
---|
| 740 | # pool,ip,custid,city,ptype,available,notes,description
|
---|
| 741 | # If desc is null, make it not null. <g>
|
---|
| 742 | if ($data[7] eq '') {
|
---|
| 743 | $data[7] = ' ';
|
---|
| 744 | }
|
---|
| 745 | # Some nice hairy Perl to decide whether to allow unassigning each IP
|
---|
| 746 | # -> if $data[5] (aka poolips.available) == 'n' then we print the unassign link
|
---|
| 747 | # else we print a blank space
|
---|
| 748 | my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[1]">$data[1]</a>),
|
---|
| 749 | $data[2],$data[5],$data[7],
|
---|
| 750 | ( ($data[5] eq 'n') ?
|
---|
| 751 | ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[1]&".
|
---|
| 752 | "alloctype=$data[4]i\">Unassign this IP</a>") :
|
---|
| 753 | (" ") )
|
---|
| 754 | );
|
---|
| 755 | printRow(\@row, 'color1') if($count%2==0);
|
---|
| 756 | printRow(\@row, 'color2') if($count%2!=0);
|
---|
| 757 | $count++;
|
---|
| 758 | }
|
---|
| 759 | print "</table>\n";
|
---|
| 760 |
|
---|
| 761 | printFooter;
|
---|
| 762 | } # end listPool
|
---|
| 763 |
|
---|
| 764 |
|
---|
| 765 | # Should this maybe just be a full static page? It just spews out some predefined HTML.
|
---|
| 766 | sub assignBlock {
|
---|
| 767 | printHeader('');
|
---|
| 768 |
|
---|
[21] | 769 | my $html;
|
---|
| 770 |
|
---|
| 771 | # New special case- block to assign is specified
|
---|
| 772 | if ($webvar{block} ne '') {
|
---|
| 773 | open HTML, "../fb-assign.html"
|
---|
| 774 | or croak "Could not open fb-assign.html: $!";
|
---|
| 775 | $html = join('',<HTML>);
|
---|
| 776 | close HTML;
|
---|
| 777 | my $block = new NetAddr::IP $webvar{block};
|
---|
| 778 | $html =~ s|\$\$BLOCK\$\$|$block|g;
|
---|
| 779 | $html =~ s|\$\$MASKBITS\$\$|$block->masklen|;
|
---|
| 780 | } else {
|
---|
| 781 | open HTML, "../assign.html"
|
---|
[4] | 782 | or croak "Could not open assign.html: $!";
|
---|
[21] | 783 | $html = join('',<HTML>);
|
---|
| 784 | close HTML;
|
---|
| 785 | }
|
---|
[4] | 786 |
|
---|
| 787 | print $html;
|
---|
| 788 |
|
---|
| 789 | printFooter();
|
---|
| 790 | } # assignBlock
|
---|
| 791 |
|
---|
| 792 |
|
---|
| 793 | # Take info on requested IP assignment and see what we can provide.
|
---|
| 794 | sub confirmAssign {
|
---|
| 795 | printHeader('');
|
---|
| 796 |
|
---|
| 797 | my $cidr;
|
---|
| 798 | my $alloc_from;
|
---|
| 799 |
|
---|
| 800 | # Going to manually validate some items.
|
---|
| 801 | # custid and city are automagic.
|
---|
| 802 | validateInput();
|
---|
| 803 |
|
---|
| 804 | # This isn't always useful.
|
---|
| 805 | # if (!$webvar{maskbits}) {
|
---|
| 806 | # printAndExit("Please enter a CIDR block length.");
|
---|
| 807 | # }
|
---|
| 808 |
|
---|
| 809 | # Several different cases here.
|
---|
| 810 | # Static IP vs netblock
|
---|
| 811 | # + Different flavours of static IP
|
---|
| 812 | # + Different flavours of netblock
|
---|
| 813 |
|
---|
| 814 | if ($webvar{alloctype} =~ /^[cdsm]i$/) {
|
---|
| 815 | my ($base,undef) = split //, $webvar{alloctype}; # split into individual chars
|
---|
| 816 | my $sql;
|
---|
| 817 | # Check for pools in Subury or North Bay if DSL or server pool. Anywhere else is
|
---|
| 818 | # invalid and shouldn't be in the db in the first place.
|
---|
| 819 | # ... aside from #^%#$%#@#^%^^!!!! legacy data. GRRR.
|
---|
| 820 | # Note that we want to retain the requested city to relate to customer info.
|
---|
| 821 | if ($base =~ /^[ds]$/) {
|
---|
| 822 | $sql = "select * from poolips where available='y' and".
|
---|
| 823 | " ptype='$base' and city='Sudbury' or city='North Bay'";
|
---|
| 824 | } else {
|
---|
| 825 | ## $city doesn't seem to get defined here.
|
---|
| 826 | my $city; # Shut up Perl's "strict" scoping/usage check.
|
---|
| 827 | $sql = "select * from poolips where available='y' and".
|
---|
| 828 | " ptype='$base' and city='$webvar{city}'";
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | # Now that we know where we're looking, we can list the pools with free IPs.
|
---|
| 832 | $sth = $ip_dbh->prepare($sql);
|
---|
| 833 | $sth->execute;
|
---|
| 834 | my %ipcount;
|
---|
| 835 | my $optionlist;
|
---|
| 836 | while (my @data = $sth->fetchrow_array) {
|
---|
| 837 | $ipcount{$data[0]}++;
|
---|
| 838 | }
|
---|
| 839 | foreach my $key (keys %ipcount) {
|
---|
| 840 | $optionlist .= "<option value='$key'>$key [$ipcount{$key} free IP(s)]</option>\n";
|
---|
| 841 | }
|
---|
| 842 | $cidr = "Single static IP";
|
---|
| 843 | $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n";
|
---|
| 844 |
|
---|
| 845 | } else { # end show pool options
|
---|
[21] | 846 |
|
---|
| 847 | if ($webvar{fbassign} eq 'y') {
|
---|
| 848 | $cidr = new NetAddr::IP $webvar{block};
|
---|
| 849 | $webvar{maskbits} = $cidr->masklen;
|
---|
| 850 | } else { # done with direct freeblocks assignment
|
---|
| 851 |
|
---|
| 852 | if (!$webvar{maskbits}) {
|
---|
| 853 | printAndExit("Please specify a CIDR mask length.");
|
---|
| 854 | }
|
---|
| 855 | my $sql;
|
---|
| 856 | my $city;
|
---|
| 857 | my $failmsg;
|
---|
| 858 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 859 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'".
|
---|
| 860 | " order by maskbits desc";
|
---|
| 861 | $failmsg = "No suitable free block found.<br>\nWe do not have a free".
|
---|
| 862 | " routeable block of that size.<br>\nYou will have to either route".
|
---|
| 863 | " a set of smaller netblocks or a single smaller netblock.";
|
---|
[4] | 864 | } else {
|
---|
[21] | 865 | if ($webvar{alloctype} =~ /^[sd]p$/) {
|
---|
| 866 | if (($webvar{city} !~ /^(Sudbury|North Bay)$/) && ($webvar{alloctype} eq 'dp')) {
|
---|
| 867 | printAndExit("You must chose Sudbury or North Bay for DSL pools."); }
|
---|
| 868 | if ($webvar{alloctype} eq 'sp') { $city = "Sudbury"; } else { $city = $webvar{city}; }
|
---|
| 869 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
| 870 | " superblock <br>\nfrom one of the master blocks in Sudbury or chose a smaller".
|
---|
| 871 | " block size for the pool.";
|
---|
| 872 | } else {
|
---|
| 873 | $city = $webvar{pop};
|
---|
| 874 | $failmsg = "No suitable free block found.<br>\nYou will have to route another".
|
---|
| 875 | " superblock to $webvar{city}<br>\nfrom one of the master blocks in Sudbury or".
|
---|
| 876 | " chose a smaller blocksize.";
|
---|
| 877 | }
|
---|
| 878 | $sql = "select * from freeblocks where city='$city' and maskbits<=$webvar{maskbits}".
|
---|
| 879 | " and routed='y' order by cidr,maskbits desc";
|
---|
[4] | 880 | }
|
---|
[21] | 881 | $sth = $ip_dbh->prepare($sql);
|
---|
| 882 | $sth->execute;
|
---|
| 883 | my @data = $sth->fetchrow_array();
|
---|
| 884 | if ($data[0] eq "") {
|
---|
| 885 | printAndExit($failmsg);
|
---|
| 886 | }
|
---|
| 887 | $cidr = new NetAddr::IP $data[0];
|
---|
| 888 | } # check for freeblocks assignment or IPDB-controlled assignment
|
---|
[4] | 889 |
|
---|
| 890 | $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">);
|
---|
| 891 |
|
---|
| 892 | # If the block to be allocated is smaller than the one we found,
|
---|
| 893 | # figure out the "real" block to be allocated.
|
---|
| 894 | if ($cidr->masklen() ne $webvar{maskbits}) {
|
---|
| 895 | my $maskbits = $cidr->masklen();
|
---|
| 896 | my @subblocks;
|
---|
| 897 | while ($maskbits++ < $webvar{maskbits}) {
|
---|
| 898 | @subblocks = $cidr->split($maskbits);
|
---|
| 899 | }
|
---|
| 900 | $cidr = $subblocks[0];
|
---|
| 901 | }
|
---|
| 902 | } # if ($webvar{alloctype} =~ /^[cdsm]i$/) {
|
---|
| 903 |
|
---|
| 904 | open HTML, "../confirm.html"
|
---|
| 905 | or croak "Could not open confirm.html: $!";
|
---|
| 906 | my $html = join '', <HTML>;
|
---|
| 907 | close HTML;
|
---|
| 908 |
|
---|
| 909 | ### gotta fix this in final
|
---|
| 910 | # Stick in customer info as necessary - if it's blank, it just ends
|
---|
| 911 | # up as blank lines ignored in the rendering of the page
|
---|
| 912 | my $custbits;
|
---|
| 913 | $html =~ s|\$\$CUSTBITS\$\$|$custbits|g;
|
---|
| 914 | ###
|
---|
| 915 |
|
---|
| 916 | # Stick in the allocation data
|
---|
| 917 | $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g;
|
---|
| 918 | $html =~ s|\$\$TYPEFULL\$\$|$full_alloc_types{$webvar{alloctype}}|g;
|
---|
| 919 | $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g;
|
---|
| 920 | $html =~ s|\$\$CIDR\$\$|$cidr|g;
|
---|
| 921 | $html =~ s|\$\$CITY\$\$|$webvar{city}|g;
|
---|
| 922 | $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g;
|
---|
| 923 | $webvar{desc} = desanitize($webvar{desc});
|
---|
| 924 | $webvar{notes} = desanitize($webvar{notes});
|
---|
| 925 | $html =~ s|\$\$DESC\$\$|$webvar{desc}|g;
|
---|
| 926 | $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g;
|
---|
| 927 | $html =~ s|\$\$ACTION\$\$|insert|g;
|
---|
| 928 |
|
---|
| 929 | print $html;
|
---|
| 930 |
|
---|
| 931 | printFooter;
|
---|
| 932 | } # end confirmAssign
|
---|
| 933 |
|
---|
| 934 |
|
---|
| 935 | # Do the work of actually inserting a block in the database.
|
---|
| 936 | sub insertAssign {
|
---|
| 937 | # Some things are done more than once.
|
---|
| 938 | printHeader('');
|
---|
| 939 | validateInput();
|
---|
| 940 |
|
---|
| 941 | # Set some things that may be needed
|
---|
| 942 | # Don't set $cidr here as it may not even be a valid IP address.
|
---|
| 943 | my $alloc_from = new NetAddr::IP $webvar{alloc_from};
|
---|
| 944 |
|
---|
| 945 | # dynDSL (dy), sIP DSL(dp), and server pools (sp) are nominally allocated to Sudbury
|
---|
| 946 | # no matter what else happens.
|
---|
| 947 | # if ($webvar{alloctype} =~ /^([sd]p|dy)$/) { $webvar{city} = "Sudbury"; }
|
---|
| 948 | # OOPS. forgot about North Bay DSL.
|
---|
| 949 | #### Gotta make this cleaner and more accurate
|
---|
| 950 | # if ($webvar{alloctype} eq "sp") { $webvar{city} = "Sudbury"; }
|
---|
| 951 |
|
---|
| 952 | # Same ordering as confirmation page
|
---|
| 953 |
|
---|
| 954 | if ($webvar{alloctype} =~ /^[cdsm]i$/) {
|
---|
| 955 | my ($base,$tmp) = split //, $webvar{alloctype}; # split into individual chars
|
---|
| 956 |
|
---|
| 957 | # We'll just have to put up with the oddities caused by SQL (un)sort order
|
---|
| 958 | $sth = $ip_dbh->prepare("select * from poolips where pool='$webvar{alloc_from}'".
|
---|
| 959 | " and available='y'");
|
---|
| 960 | $sth->execute;
|
---|
| 961 |
|
---|
| 962 | my @data = $sth->fetchrow_array;
|
---|
| 963 | my $cidr = $data[1];
|
---|
| 964 |
|
---|
| 965 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}',available='n'".
|
---|
| 966 | " where ip='$cidr'");
|
---|
| 967 | $sth->execute;
|
---|
| 968 | if ($sth->err) {
|
---|
| 969 | printAndExit("Allocation of $cidr to $webvar{custid} failed: '".$sth->errstr."'");
|
---|
[17] | 970 | syslog "err", "Allocation of $cidr to $webvar{custid} by $authuser failed: ".
|
---|
[4] | 971 | "'".$sth->errstr."'";
|
---|
| 972 | }
|
---|
| 973 | print qq(<div class="center"><div class="heading">The IP $cidr has been allocated to customer $webvar{custid}</div></div>);
|
---|
| 974 | syslog "notice", "$authuser allocated $cidr to $webvar{custid}";
|
---|
| 975 |
|
---|
| 976 | } else { # end IP-from-pool allocation
|
---|
| 977 |
|
---|
| 978 | # Set $cidr here as it may not be a valid IP address elsewhere.
|
---|
| 979 | my $cidr = new NetAddr::IP $webvar{fullcidr};
|
---|
| 980 |
|
---|
| 981 | # Allow transactions, and make errors much easier to catch.
|
---|
| 982 | # Much as I would like to error-track specifically on each ->execute,
|
---|
| 983 | # that's a LOT of code. :/
|
---|
| 984 | $ip_dbh->{AutoCommit} = 0;
|
---|
| 985 | $ip_dbh->{RaiseError} = 1;
|
---|
| 986 |
|
---|
| 987 | if ($webvar{fullcidr} eq $webvar{alloc_from}) {
|
---|
| 988 | # Easiest case- insert in one table, delete in the other, and go home. More or less.
|
---|
| 989 | # insert into allocations values (cidr,custid,type,city,desc) and
|
---|
| 990 | # delete from freeblocks where cidr='cidr'
|
---|
| 991 | # For data safety on non-transaction DBs, we delete first.
|
---|
| 992 |
|
---|
| 993 | eval {
|
---|
| 994 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 995 | $sth = $ip_dbh->prepare("update freeblocks set routed='y',city='$webvar{city}'".
|
---|
| 996 | " where cidr='$webvar{fullcidr}'");
|
---|
| 997 | $sth->execute;
|
---|
| 998 | $sth = $ip_dbh->prepare("insert into routed values ('$webvar{fullcidr}',".
|
---|
| 999 | $cidr->masklen.",'$webvar{city}')");
|
---|
| 1000 | $sth->execute;
|
---|
| 1001 | } else {
|
---|
| 1002 | # common stuff for end-use, dialup, dynDSL, pools, etc, etc.
|
---|
| 1003 |
|
---|
| 1004 | # city has to be reset for DSL/server pools; nominally to Sudbury.
|
---|
| 1005 | ## Gotta rethink this; DSL pools can be in North Bay as well. :/
|
---|
| 1006 | #if ($webvar{alloctype} =~ /^[sd]p$/) { $webvar{city} = 'Sudbury'; }
|
---|
| 1007 |
|
---|
| 1008 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{fullcidr}'");
|
---|
| 1009 | $sth->execute;
|
---|
| 1010 |
|
---|
| 1011 | $sth = $ip_dbh->prepare("insert into allocations values ('$webvar{fullcidr}',".
|
---|
| 1012 | "'$webvar{custid}','$webvar{alloctype}','$webvar{city}','$webvar{desc}',".
|
---|
| 1013 | "'$webvar{notes}',".$cidr->masklen.")");
|
---|
| 1014 | $sth->execute;
|
---|
| 1015 | } # routing vs non-routing netblock
|
---|
| 1016 | $ip_dbh->commit;
|
---|
| 1017 | }; # end of eval
|
---|
| 1018 | if ($@) {
|
---|
| 1019 | carp "Transaction aborted because $@";
|
---|
| 1020 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1021 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
[4] | 1022 | "'$webvar{alloctype}' by $authuser failed: '$@'";
|
---|
| 1023 | printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
|
---|
| 1024 | }
|
---|
| 1025 |
|
---|
| 1026 | # If we get here, the DB transaction has succeeded.
|
---|
| 1027 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as '$webvar{alloctype}'";
|
---|
| 1028 |
|
---|
| 1029 | # How to log SQL without munging too many error-checking wrappers in?
|
---|
| 1030 | # syslog "info", "
|
---|
| 1031 | # We don't. GRRR.
|
---|
| 1032 |
|
---|
| 1033 | } else { # webvar{fullcidr} != webvar{alloc_from}
|
---|
| 1034 | # Hard case. Allocation is smaller than free block.
|
---|
| 1035 | my $wantmaskbits = $cidr->masklen;
|
---|
| 1036 | my $maskbits = $alloc_from->masklen;
|
---|
| 1037 |
|
---|
| 1038 | my @newfreeblocks; # Holds free blocks generated from splitting the source freeblock.
|
---|
| 1039 |
|
---|
| 1040 | my $i=0;
|
---|
| 1041 | while ($maskbits++ < $wantmaskbits) {
|
---|
| 1042 | my @subblocks = $alloc_from->split($maskbits);
|
---|
| 1043 | $newfreeblocks[$i++] = $subblocks[1];
|
---|
| 1044 | } # while
|
---|
| 1045 |
|
---|
| 1046 | # Begin SQL transaction block
|
---|
| 1047 | eval {
|
---|
| 1048 | # Delete old freeblocks entry
|
---|
| 1049 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{alloc_from}'");
|
---|
| 1050 | $sth->execute();
|
---|
| 1051 |
|
---|
| 1052 | # now we have to do some magic for routing blocks
|
---|
| 1053 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 1054 | # Insert the new freeblocks entries
|
---|
| 1055 | # Note that non-routed blocks are assigned to <NULL>
|
---|
| 1056 | $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, '<NULL>','n')");
|
---|
| 1057 | foreach my $block (@newfreeblocks) {
|
---|
| 1058 | $sth->execute("$block", $block->masklen);
|
---|
| 1059 | }
|
---|
| 1060 | # Insert the entry in the routed table
|
---|
| 1061 | $sth = $ip_dbh->prepare("insert into routed values ('$cidr',".
|
---|
| 1062 | $cidr->masklen.",'$webvar{city}')");
|
---|
| 1063 | $sth->execute;
|
---|
| 1064 | # Insert the (almost) same entry in the freeblocks table
|
---|
| 1065 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".
|
---|
| 1066 | $cidr->masklen.",'$webvar{city}','y')");
|
---|
| 1067 | $sth->execute;
|
---|
| 1068 |
|
---|
| 1069 | } else { # done with alloctype == rr
|
---|
| 1070 |
|
---|
| 1071 | # Insert the new freeblocks entries
|
---|
| 1072 | $sth = $ip_dbh->prepare("insert into freeblocks values (?, ?, ?,'y')");
|
---|
| 1073 | foreach my $block (@newfreeblocks) {
|
---|
| 1074 | $sth->execute("$block", $block->masklen, $webvar{city});
|
---|
| 1075 | }
|
---|
| 1076 | # Insert the allocations entry
|
---|
| 1077 | $sth = $ip_dbh->prepare("insert into allocations values ('$webvar{fullcidr}',".
|
---|
| 1078 | "'$webvar{custid}','$webvar{alloctype}','$webvar{city}',".
|
---|
| 1079 | "'$webvar{desc}','$webvar{notes}',".$cidr->masklen.")");
|
---|
| 1080 | $sth->execute;
|
---|
| 1081 | } # done with netblock alloctype != rr
|
---|
| 1082 | $ip_dbh->commit;
|
---|
| 1083 | }; # end eval
|
---|
| 1084 | if ($@) {
|
---|
| 1085 | carp "Transaction aborted because $@";
|
---|
| 1086 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1087 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ".
|
---|
[4] | 1088 | "'$webvar{alloctype}' by $authuser failed: '$@'";
|
---|
| 1089 | printAndExit("Allocation of $cidr as $full_alloc_types{$webvar{alloctype}} failed.\n");
|
---|
| 1090 | }
|
---|
| 1091 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as '$webvar{alloctype}'";
|
---|
| 1092 |
|
---|
| 1093 | } # end fullcidr != alloc_from
|
---|
| 1094 |
|
---|
| 1095 | # Begin SQL transaction block
|
---|
| 1096 | eval {
|
---|
| 1097 | # special extra handling for pools.
|
---|
| 1098 | # Note that this must be done for ANY pool allocation!
|
---|
| 1099 | if ( my ($pooltype) = ($webvar{alloctype} =~ /^([cdsm])p$/) ) {
|
---|
| 1100 | # have to insert all pool IPs into poolips table as "unallocated".
|
---|
| 1101 | $sth = $ip_dbh->prepare("insert into poolips values ('$webvar{fullcidr}',".
|
---|
| 1102 | " ?, '6750400', '$webvar{city}', '$pooltype', 'y', '')");
|
---|
| 1103 | my @poolip_list = $cidr->hostenum;
|
---|
| 1104 | for (my $i=1; $i<=$#poolip_list; $i++) {
|
---|
| 1105 | $sth->execute($poolip_list[$i]->addr);
|
---|
| 1106 | }
|
---|
| 1107 | } # end pool special
|
---|
| 1108 | $ip_dbh->commit;
|
---|
| 1109 | }; # end eval
|
---|
| 1110 | if ($@) {
|
---|
| 1111 | carp "Transaction aborted because $@";
|
---|
| 1112 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1113 | syslog "err", "Initialization of pool '$webvar{fullcidr}' by $authuser failed: '$@'";
|
---|
[4] | 1114 | printAndExit("$full_alloc_types{$webvar{alloctype}} $webvar{fullcidr} not completely initialized.");
|
---|
| 1115 | }
|
---|
| 1116 | syslog "notice", "$full_alloc_types{$webvar{alloctype}} '$webvar{fullcidr}' successfully initialized by $authuser";
|
---|
| 1117 |
|
---|
| 1118 | # Turn off transactions and exception-on-error'ing
|
---|
| 1119 | $ip_dbh->{AutoCommit} = 0;
|
---|
| 1120 | $ip_dbh->{RaiseError} = 1;
|
---|
| 1121 |
|
---|
| 1122 | 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>);
|
---|
| 1123 |
|
---|
| 1124 | } # end static-IP vs netblock allocation
|
---|
| 1125 |
|
---|
| 1126 | printFooter();
|
---|
| 1127 | } # end insertAssign()
|
---|
| 1128 |
|
---|
| 1129 |
|
---|
| 1130 | # Does some basic checks on common input data to make sure nothing
|
---|
| 1131 | # *really* weird gets in to the database through this script.
|
---|
| 1132 | # Does NOT do complete input validation!!!
|
---|
| 1133 | sub validateInput {
|
---|
| 1134 | if ($webvar{city} eq '-') {
|
---|
| 1135 | printAndExit("Please choose a city.");
|
---|
| 1136 | }
|
---|
| 1137 | chomp $webvar{alloctype};
|
---|
| 1138 | # We have different handling for customer allocations and "internal" or "our" allocations
|
---|
| 1139 | if ($webvar{alloctype} =~ /^(ci|di|cn|mi)$/) {
|
---|
| 1140 | if (!$webvar{custid}) {
|
---|
| 1141 | printAndExit("Please enter a customer ID.");
|
---|
| 1142 | }
|
---|
[23] | 1143 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
[4] | 1144 | printAndExit("Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for static IPs for staff.");
|
---|
| 1145 | }
|
---|
| 1146 | print "<!-- [ In validateInput(). Insert customer ID cross-check here. ] -->\n";
|
---|
[12] | 1147 | } elsif ($webvar{alloctype} =~ /^([sdcmw]p|si|dn|dy|dc|ee|rr|ii)$/){
|
---|
[4] | 1148 | # All non-customer allocations MUST be entered with "our" customer ID.
|
---|
| 1149 | # I have Defined this as 6750400 for consistency.
|
---|
| 1150 | $webvar{custid} = "6750400";
|
---|
| 1151 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 1152 | if ($webvar{city} !~ /^(?:Huntsville|North Bay|Ottawa|Pembroke|Sault Ste. Marie|Sudbury|Timmins|Toronto)$/) {
|
---|
| 1153 | printAndExit("Please choose a valid POP location for a routed netblock. Valid ".
|
---|
| 1154 | "POP locations are currently:<br>\n Huntsville North Bay Ottawa Pembroke ".
|
---|
| 1155 | "Sault Ste. Marie Sudbury Timmins Toronto");
|
---|
| 1156 | }
|
---|
| 1157 | }
|
---|
| 1158 | } else {
|
---|
| 1159 | # Danger! Danger! alloctype should ALWAYS be set by a dropdown. Anyone
|
---|
| 1160 | # managing to call things in such a way as to cause this deserves a cryptic error.
|
---|
| 1161 | printAndExit("Invalid alloctype");
|
---|
| 1162 | }
|
---|
| 1163 | return 0;
|
---|
| 1164 | } # end validateInput
|
---|
| 1165 |
|
---|
| 1166 |
|
---|
| 1167 | # Displays details of a specific allocation in a form
|
---|
| 1168 | # Allows update/delete
|
---|
| 1169 | # action=edit
|
---|
| 1170 | sub edit {
|
---|
| 1171 | printHeader('');
|
---|
| 1172 |
|
---|
| 1173 | my $sql;
|
---|
| 1174 |
|
---|
| 1175 | # Two cases: block is a netblock, or block is a static IP from a pool
|
---|
| 1176 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data
|
---|
| 1177 | if ($webvar{block} =~ /\/32$/) {
|
---|
| 1178 | $sql = "select ip,custid,ptype,city,description,notes from poolips where ip='$webvar{block}'";
|
---|
| 1179 | } else {
|
---|
| 1180 | $sql = "select cidr,custid,type,city,description,notes from allocations where cidr='$webvar{block}'"
|
---|
| 1181 | }
|
---|
| 1182 |
|
---|
| 1183 | # gotta snag block info from db
|
---|
| 1184 | $sth = $ip_dbh->prepare($sql);
|
---|
| 1185 | $sth->execute;
|
---|
| 1186 | my @data = $sth->fetchrow_array;
|
---|
| 1187 |
|
---|
| 1188 | # Clean up extra whitespace on alloc type
|
---|
| 1189 | $data[2] =~ s/\s//;
|
---|
| 1190 |
|
---|
| 1191 | # Postfix "i" on pool IP types
|
---|
| 1192 | if ($data[2] =~ /^[cdsm]$/) {
|
---|
| 1193 | $data[2] .= "i";
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
| 1196 | open (HTML, "../editDisplay.html")
|
---|
| 1197 | or croak "Could not open editDisplay.html :$!";
|
---|
| 1198 | my $html = join('', <HTML>);
|
---|
| 1199 |
|
---|
| 1200 | # We can't let the city be changed here; this block is a part of
|
---|
| 1201 | # a larger routed allocation and therefore by definition can't be moved.
|
---|
| 1202 | # block and city are static.
|
---|
| 1203 | ##fixme
|
---|
| 1204 | # Needs thinking. Have to allow changes to city to correct errors, no?
|
---|
| 1205 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
| 1206 | $html =~ s/\$\$CITY\$\$/$data[3]/g;
|
---|
| 1207 |
|
---|
| 1208 | # Screw it. Changing allocation types gets very ugly VERY quickly- especially
|
---|
| 1209 | # with the much longer list of allocation types.
|
---|
| 1210 | # We'll just show what type of block it is.
|
---|
| 1211 |
|
---|
| 1212 | $html =~ s/\$\$TYPE\$\$/$data[2]/g;
|
---|
| 1213 | $html =~ s/\$\$FULLTYPE\$\$/$full_alloc_types{$data[2]}/g;
|
---|
| 1214 |
|
---|
| 1215 | # These can be modified, although CustID changes may get ignored.
|
---|
| 1216 | $html =~ s/\$\$CUSTID\$\$/$data[1]/g;
|
---|
| 1217 | $html =~ s/\$\$DESC\$\$/$data[4]/g;
|
---|
| 1218 | $html =~ s/\$\$NOTES\$\$/$data[5]/g;
|
---|
| 1219 |
|
---|
| 1220 | print $html;
|
---|
| 1221 |
|
---|
| 1222 | printFooter();
|
---|
| 1223 | } # edit()
|
---|
| 1224 |
|
---|
| 1225 |
|
---|
| 1226 | # Stuff new info about a block into the db
|
---|
| 1227 | # action=update
|
---|
| 1228 | sub update {
|
---|
| 1229 | printHeader('');
|
---|
| 1230 |
|
---|
| 1231 | # Make sure incoming data is in correct format - custID among other things.
|
---|
| 1232 | validateInput;
|
---|
| 1233 |
|
---|
| 1234 | # SQL transaction wrapper
|
---|
| 1235 | eval {
|
---|
| 1236 | # Relatively simple SQL transaction here.
|
---|
| 1237 | my $sql;
|
---|
| 1238 | if (my $pooltype = ($webvar{alloctype} =~ /^([cdms])i$/) ) {
|
---|
| 1239 | $sql = "update poolips set custid='$webvar{custid}',".
|
---|
| 1240 | "notes='$webvar{notes}',description='$webvar{desc}' ".
|
---|
| 1241 | "where ip='$webvar{block}'";
|
---|
| 1242 | } else {
|
---|
| 1243 | $sql = "update allocations set custid='$webvar{custid}',".
|
---|
[20] | 1244 | "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}' ".
|
---|
[4] | 1245 | "where cidr='$webvar{block}'";
|
---|
| 1246 | }
|
---|
| 1247 | syslog "debug", $sql;
|
---|
| 1248 | $sth = $ip_dbh->prepare($sql);
|
---|
| 1249 | $sth->execute;
|
---|
| 1250 | $ip_dbh->commit;
|
---|
| 1251 | };
|
---|
| 1252 | if ($@) {
|
---|
| 1253 | carp "Transaction aborted because $@";
|
---|
| 1254 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1255 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
|
---|
[4] | 1256 | printAndExit("Could not update block/IP $webvar{block}");
|
---|
| 1257 | }
|
---|
| 1258 |
|
---|
| 1259 | # If we get here, the operation succeeded.
|
---|
| 1260 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
| 1261 | open (HTML, "../updated.html")
|
---|
| 1262 | or croak "Could not open updated.html :$!";
|
---|
| 1263 | my $html = join('', <HTML>);
|
---|
| 1264 |
|
---|
| 1265 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g;
|
---|
| 1266 | $html =~ s/\$\$CITY\$\$/$webvar{city}/g;
|
---|
| 1267 | $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g;
|
---|
| 1268 | $html =~ s/\$\$TYPEFULL\$\$/$full_alloc_types{$webvar{alloctype}}/g;
|
---|
| 1269 | $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g;
|
---|
| 1270 | $html =~ s/\$\$DESC\$\$/$webvar{desc}/g;
|
---|
| 1271 | $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g;
|
---|
| 1272 |
|
---|
| 1273 | print $html;
|
---|
| 1274 |
|
---|
| 1275 | printFooter;
|
---|
| 1276 | } # update()
|
---|
| 1277 |
|
---|
| 1278 |
|
---|
| 1279 | # Delete an allocation.
|
---|
| 1280 | sub remove
|
---|
| 1281 | {
|
---|
| 1282 | printHeader('');
|
---|
| 1283 | #show confirm screen.
|
---|
| 1284 | open HTML, "../confirmRemove.html"
|
---|
| 1285 | or croak "Could not open confirmRemove.html :$!";
|
---|
| 1286 | my $html = join('', <HTML>);
|
---|
| 1287 | close HTML;
|
---|
| 1288 |
|
---|
| 1289 | # Serves'em right for getting here...
|
---|
| 1290 | if (!defined($webvar{block})) {
|
---|
| 1291 | printAndExit("Error 332");
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | my ($cidr, $custid, $type, $city, $desc, $notes, $alloctype);
|
---|
| 1295 |
|
---|
| 1296 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 1297 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'");
|
---|
| 1298 | $sth->execute();
|
---|
| 1299 |
|
---|
| 1300 | # This feels... extreme.
|
---|
| 1301 | croak $sth->errstr() if($sth->errstr());
|
---|
| 1302 |
|
---|
| 1303 | $sth->bind_columns(\$cidr,\$city);
|
---|
| 1304 | $sth->execute();
|
---|
| 1305 | $sth->fetch || croak $sth->errstr();
|
---|
| 1306 | $custid = "N/A";
|
---|
| 1307 | $alloctype = $webvar{alloctype};
|
---|
| 1308 | $desc = "N/A";
|
---|
| 1309 | $notes = "N/A";
|
---|
| 1310 |
|
---|
| 1311 | } elsif ($webvar{alloctype} eq 'mm') {
|
---|
| 1312 | $cidr = $webvar{block};
|
---|
| 1313 | $city = "N/A";
|
---|
| 1314 | $custid = "N/A";
|
---|
| 1315 | $alloctype = $webvar{alloctype};
|
---|
| 1316 | $desc = "N/A";
|
---|
| 1317 | $notes = "N/A";
|
---|
[12] | 1318 | } elsif ($webvar{alloctype} =~ /^[sdcmw]i$/) { # done with alloctype=rr
|
---|
[4] | 1319 |
|
---|
| 1320 | # Unassigning a static IP
|
---|
| 1321 | my $sth = $ip_dbh->prepare("select ip,custid,city,ptype,notes from poolips".
|
---|
| 1322 | " where ip='$webvar{block}'");
|
---|
| 1323 | $sth->execute();
|
---|
| 1324 | # croak $sth->errstr() if($sth->errstr());
|
---|
| 1325 |
|
---|
| 1326 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes);
|
---|
| 1327 | $sth->fetch() || croak $sth->errstr;
|
---|
| 1328 |
|
---|
| 1329 | $alloctype .="i";
|
---|
| 1330 |
|
---|
[12] | 1331 | } else { # done with alloctype=[sdcmw]i
|
---|
[4] | 1332 |
|
---|
| 1333 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,description,notes from ".
|
---|
| 1334 | "allocations where cidr='$webvar{block}'");
|
---|
| 1335 | $sth->execute();
|
---|
| 1336 | # croak $sth->errstr() if($sth->errstr());
|
---|
| 1337 |
|
---|
| 1338 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$desc, \$notes);
|
---|
| 1339 | $sth->fetch() || croak $sth->errstr;
|
---|
| 1340 | } # end cases for different alloctypes
|
---|
| 1341 |
|
---|
| 1342 | # Munge everything into HTML
|
---|
| 1343 | $html =~ s|Please confirm|Please confirm <b>removal</b> of|;
|
---|
| 1344 | $html =~ s|\$\$BLOCK\$\$|$cidr|g;
|
---|
| 1345 | $html =~ s|\$\$TYPEFULL\$\$|$full_alloc_types{$alloctype}|g;
|
---|
| 1346 | $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g;
|
---|
| 1347 | $html =~ s|\$\$CITY\$\$|$city|g;
|
---|
| 1348 | $html =~ s|\$\$CUSTID\$\$|$custid|g;
|
---|
| 1349 | $html =~ s|\$\$DESC\$\$|$desc|g;
|
---|
| 1350 | $html =~ s|\$\$NOTES\$\$|$notes|g;
|
---|
| 1351 |
|
---|
| 1352 | $html =~ s|\$\$ACTION\$\$|finaldelete|g;
|
---|
| 1353 |
|
---|
| 1354 | # Set the warning text.
|
---|
[12] | 1355 | if ($alloctype =~ /^[sdcmw]p$/) {
|
---|
[4] | 1356 | $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>|;
|
---|
| 1357 | } else {
|
---|
| 1358 | $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|;
|
---|
| 1359 | }
|
---|
| 1360 |
|
---|
| 1361 | print $html;
|
---|
| 1362 | printFooter;
|
---|
| 1363 | } # end edit()
|
---|
| 1364 |
|
---|
| 1365 |
|
---|
| 1366 | # Delete an allocation. Return it to the freeblocks table; munge
|
---|
| 1367 | # data as necessary to keep as few records as possible in freeblocks
|
---|
| 1368 | # to prevent weirdness when allocating blocks later.
|
---|
| 1369 | # Remove IPs from pool listing if necessary
|
---|
| 1370 | sub finalDelete {
|
---|
| 1371 | printHeader('');
|
---|
| 1372 |
|
---|
| 1373 | # Enable transactions and exception-on-errors... but only for this sub
|
---|
| 1374 | local $ip_dbh->{AutoCommit} = 0;
|
---|
| 1375 | local $ip_dbh->{RaiseError} = 1;
|
---|
| 1376 |
|
---|
[12] | 1377 | if ($webvar{alloctype} =~ /^[sdcmw]i$/) {
|
---|
[4] | 1378 |
|
---|
| 1379 | eval {
|
---|
| 1380 | $sth = $ip_dbh->prepare("select * from poolips where ip='$webvar{block}'");
|
---|
| 1381 | $sth->execute;
|
---|
| 1382 | my @data = $sth->fetchrow_array;
|
---|
| 1383 | $sth = $ip_dbh->prepare("select city from allocations where cidr='$data[0]'");
|
---|
| 1384 | $sth->execute;
|
---|
| 1385 | @data = $sth->fetchrow_array;
|
---|
| 1386 | $sth = $ip_dbh->prepare("update poolips set custid='6750400', available='y',".
|
---|
| 1387 | " city='$data[0]' where ip='$webvar{block}'");
|
---|
| 1388 | $sth->execute;
|
---|
| 1389 | $ip_dbh->commit;
|
---|
| 1390 | };
|
---|
| 1391 | if ($@) {
|
---|
| 1392 | carp "Transaction aborted because $@";
|
---|
| 1393 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1394 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$@'";
|
---|
[4] | 1395 | printAndExit("Could not deallocate static IP $webvar{block}");
|
---|
| 1396 | }
|
---|
| 1397 | print "<div class=heading align=center>Success! $webvar{block} deallocated.</div>\n";
|
---|
| 1398 | syslog "notice", "$authuser deallocated static IP $webvar{block}";
|
---|
| 1399 |
|
---|
[12] | 1400 | } elsif ($webvar{alloctype} eq 'mm') { # end alloctype = [sdcmw]i
|
---|
[4] | 1401 |
|
---|
| 1402 | eval {
|
---|
| 1403 | $sth = $ip_dbh->prepare("delete from masterblocks where cidr='$webvar{block}'");
|
---|
| 1404 | $sth->execute;
|
---|
| 1405 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr='$webvar{block}'");
|
---|
| 1406 | $sth->execute;
|
---|
| 1407 | $ip_dbh->commit;
|
---|
| 1408 | };
|
---|
| 1409 | if ($@) {
|
---|
| 1410 | carp "Transaction aborted because $@";
|
---|
| 1411 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1412 | syslog "err", "$authuser could not remove master block '$webvar{block}': '$@'";
|
---|
[4] | 1413 | printAndExit("Could not remove master block $webvar{block}");
|
---|
| 1414 | }
|
---|
| 1415 | print "<div class=heading align=center>Success! Master $webvar{block} removed.</div>\n";
|
---|
| 1416 | syslog "notice", "$authuser removed master block $webvar{block}";
|
---|
| 1417 |
|
---|
| 1418 | } else { # end alloctype master block case
|
---|
| 1419 |
|
---|
| 1420 | ## This is a big block; but it HAS to be done in a chunk. Any removal
|
---|
| 1421 | ## of a netblock allocation may result in a larger chunk of free
|
---|
| 1422 | ## contiguous IP space - which may in turn be combined into a single
|
---|
| 1423 | ## netblock rather than a number of smaller netblocks.
|
---|
| 1424 |
|
---|
| 1425 | eval {
|
---|
| 1426 |
|
---|
| 1427 | my $cidr = new NetAddr::IP $webvar{block};
|
---|
| 1428 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 1429 |
|
---|
| 1430 | $sth = $ip_dbh->prepare("delete from routed where cidr='$webvar{block}'");
|
---|
| 1431 | $sth->execute;
|
---|
| 1432 | # Make sure block getting deleted is properly accounted for.
|
---|
| 1433 | $sth = $ip_dbh->prepare("update freeblocks set routed='n',city='<NULL>'".
|
---|
| 1434 | " where cidr='$webvar{block}'");
|
---|
| 1435 | $sth->execute;
|
---|
| 1436 | # Set up query to start compacting free blocks.
|
---|
| 1437 | $sth = $ip_dbh->prepare("select * from freeblocks where ".
|
---|
| 1438 | "maskbits<=".$cidr->masklen." and routed='n' order by maskbits desc");
|
---|
| 1439 |
|
---|
| 1440 | } else { # end alloctype routing case
|
---|
| 1441 |
|
---|
| 1442 | $sth = $ip_dbh->prepare("delete from allocations where cidr='$webvar{block}'");
|
---|
| 1443 | $sth->execute;
|
---|
| 1444 | # Special case - delete pool IPs
|
---|
[12] | 1445 | if ($webvar{alloctype} =~ /^[sdcmw]p$/) {
|
---|
[4] | 1446 | # We have to delete the IPs from the pool listing.
|
---|
[13] | 1447 | $sth = $ip_dbh->prepare("delete from poolips where pool='$webvar{block}'");
|
---|
[4] | 1448 | $sth->execute;
|
---|
| 1449 | }
|
---|
| 1450 |
|
---|
| 1451 | # Set up query for compacting free blocks.
|
---|
| 1452 | $sth = $ip_dbh->prepare("select * from freeblocks where city='$webvar{city}'".
|
---|
| 1453 | " and maskbits<=".$cidr->masklen." and routed='y' order by maskbits desc");
|
---|
| 1454 |
|
---|
| 1455 | } # end alloctype general case
|
---|
| 1456 |
|
---|
| 1457 | # Now we look for larger-or-equal-sized free blocks in the same master (routed)
|
---|
| 1458 | # (super)block. If there aren't any, we can't combine blocks anyway. If there
|
---|
| 1459 | # are, we check to see if we can combine blocks.
|
---|
| 1460 | # Execute the statement prepared in the if-else above.
|
---|
| 1461 |
|
---|
| 1462 | $sth->execute;
|
---|
| 1463 |
|
---|
| 1464 | # NetAddr::IP->compact() attempts to produce the smallest inclusive block
|
---|
| 1465 | # from the caller and the passed terms.
|
---|
| 1466 | # EG: if you call $cidr->compact($ip1,$ip2,$ip3) when $cidr, $ip1, $ip2,
|
---|
| 1467 | # and $ip3 are consecutive /27's starting on .0 (.0-.31, .32-.63,
|
---|
| 1468 | # .64-.95, and .96-.128), you will get an array containing a single
|
---|
| 1469 | # /25 as element 0 (.0-.127). Order is not important; you could have
|
---|
| 1470 | # $cidr=.32/27, $ip1=.96/27, $ip2=.0/27, and $ip3=.64/27.
|
---|
| 1471 |
|
---|
| 1472 | my (@together, @combinelist);
|
---|
| 1473 | my $i=0;
|
---|
| 1474 | while (my @data = $sth->fetchrow_array) {
|
---|
| 1475 | my $testIP = new NetAddr::IP $data[0];
|
---|
| 1476 | @together = $testIP->compact($cidr);
|
---|
| 1477 | my $num = @together;
|
---|
| 1478 | if ($num == 1) {
|
---|
| 1479 | $cidr = $together[0];
|
---|
| 1480 | $combinelist[$i++] = $testIP;
|
---|
| 1481 | }
|
---|
| 1482 | }
|
---|
| 1483 |
|
---|
| 1484 | # Clear old freeblocks entries - if any. $i==0 if not.
|
---|
| 1485 | if ($i>0) {
|
---|
| 1486 | $sth = $ip_dbh->prepare("delete from freeblocks where cidr=?");
|
---|
| 1487 | foreach my $block (@combinelist) {
|
---|
[13] | 1488 | $sth->execute("$block");
|
---|
[4] | 1489 | }
|
---|
| 1490 | }
|
---|
| 1491 |
|
---|
| 1492 | # insert "new" freeblocks entry
|
---|
| 1493 | if ($webvar{alloctype} eq 'rr') {
|
---|
| 1494 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".$cidr->masklen.
|
---|
| 1495 | ",'<NULL>','n')");
|
---|
| 1496 | } else {
|
---|
| 1497 | $sth = $ip_dbh->prepare("insert into freeblocks values ('$cidr',".$cidr->masklen.
|
---|
| 1498 | ",'$webvar{city}','y')");
|
---|
| 1499 | }
|
---|
| 1500 | $sth->execute;
|
---|
| 1501 |
|
---|
| 1502 | # If we got here, we've succeeded. Whew!
|
---|
| 1503 | $ip_dbh->commit;
|
---|
| 1504 | }; # end eval
|
---|
| 1505 | if ($@) {
|
---|
| 1506 | carp "Transaction aborted because $@";
|
---|
| 1507 | eval { $ip_dbh->rollback; };
|
---|
[17] | 1508 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$@'";
|
---|
[4] | 1509 | printAndExit("Could not deallocate netblock $webvar{block}");
|
---|
| 1510 | }
|
---|
| 1511 | print "<div class=heading align=center>Success! $webvar{block} deleted.</div>\n";
|
---|
[22] | 1512 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}";
|
---|
[4] | 1513 |
|
---|
| 1514 | } # end alloctype != netblock
|
---|
| 1515 |
|
---|
| 1516 | printFooter;
|
---|
| 1517 | } # finalDelete
|
---|
| 1518 |
|
---|
| 1519 |
|
---|
| 1520 | # Just in case we manage to get here.
|
---|
| 1521 | exit 0;
|
---|