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