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