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