| [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-03-10 22:13:59 +0000 (Thu, 10 Mar 2005) $ | 
|---|
|  | 7 | # SVN revision $Rev: 194 $ | 
|---|
|  | 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); | 
|---|
| [158] | 16 | use MyIPDB; | 
|---|
| [56] | 17 | use CustIDCK; | 
|---|
| [4] | 18 | use POSIX qw(ceil); | 
|---|
|  | 19 | use NetAddr::IP; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | use Sys::Syslog; | 
|---|
|  | 22 |  | 
|---|
|  | 23 | openlog "IPDB","pid","local2"; | 
|---|
|  | 24 |  | 
|---|
|  | 25 | # Collect the username from HTTP auth.  If undefined, we're in a test environment. | 
|---|
|  | 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 |  | 
|---|
| [125] | 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; | 
|---|
| [158] | 40 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
| [125] | 41 | if (!$ip_dbh) { | 
|---|
| [185] | 42 | printAndExit("Database error: $errstr\n"); | 
|---|
| [125] | 43 | } | 
|---|
|  | 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 |  | 
|---|
| [99] | 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 |  | 
|---|
| [125] | 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 |  | 
|---|
| [159] | 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: '$@'"; | 
|---|
| [125] | 100 | printError("Could not add master block $webvar{cidr} to database: $@"); | 
|---|
|  | 101 | } else { | 
|---|
|  | 102 | print "<div type=heading align=center>Success!</div>\n"; | 
|---|
|  | 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 | } | 
|---|
| [125] | 165 | ## Finally! Done with that NASTY "case" emulation! | 
|---|
| [4] | 166 |  | 
|---|
|  | 167 |  | 
|---|
|  | 168 |  | 
|---|
| [125] | 169 | # Clean up IPDB globals, DB handle, etc. | 
|---|
|  | 170 | finish($ip_dbh); | 
|---|
|  | 171 | # We print the footer here, so we don't have to do it elsewhere. | 
|---|
|  | 172 | printFooter; | 
|---|
|  | 173 | # Just in case something waaaayyy down isn't in place | 
|---|
|  | 174 | # properly... we exit explicitly. | 
|---|
|  | 175 | exit; | 
|---|
| [4] | 176 |  | 
|---|
|  | 177 |  | 
|---|
| [125] | 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); | 
|---|
| [174] | 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.  :/ | 
|---|
| [174] | 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). | 
|---|
| [174] | 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> | 
|---|
| [174] | 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 | 
|---|
| [174] | 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; | 
|---|
| [174] | 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]; | 
|---|
| [174] | 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"; | 
|---|
| [174] | 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... | 
|---|
| [125] | 278 | printError("Invalid query."); | 
|---|
| [4] | 279 | } | 
|---|
|  | 280 | } else { | 
|---|
|  | 281 | # This shouldn't happen, but if it does, whoever gets it deserves what they get... | 
|---|
| [125] | 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 |  | 
|---|
| [134] | 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) { | 
|---|
| [192] | 327 | # cidr,custid,type,city,description | 
|---|
|  | 328 | # Prefix subblocks with "Sub " | 
|---|
|  | 329 | my @row = ( (($data[2] =~ /^.r$/) ? 'Sub ' : ''). | 
|---|
|  | 330 | qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), | 
|---|
| [98] | 331 | $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]); | 
|---|
| [4] | 332 | # Allow listing of pool if desired/required. | 
|---|
| [159] | 333 | if ($data[2] =~ /^.[pd]$/) { | 
|---|
| [4] | 334 | $row[0] .= '   <a href="/ip/cgi-bin/main.cgi?action=listpool'. | 
|---|
|  | 335 | "&pool=$data[0]\">List IPs</a>"; | 
|---|
|  | 336 | } | 
|---|
|  | 337 | printRow(\@row, 'color1', 1) if ($count%2==0); | 
|---|
|  | 338 | printRow(\@row, 'color2', 1) if ($count%2!=0); | 
|---|
|  | 339 | $count++; | 
|---|
|  | 340 | } | 
|---|
|  | 341 |  | 
|---|
|  | 342 | # Have to think on this call, it's primarily to clean up unfetched rows from a select. | 
|---|
|  | 343 | # In this context it's probably a good idea. | 
|---|
|  | 344 | $sth->finish(); | 
|---|
|  | 345 |  | 
|---|
|  | 346 | my $upper = $offset+$count; | 
|---|
|  | 347 | print "<tr><td colspan=10 bgcolor=white class=regular>Records found: $rowCount<br><i>Displaying: $offset - $upper</i></td></tr>\n"; | 
|---|
|  | 348 | print "</table></center>\n"; | 
|---|
|  | 349 |  | 
|---|
|  | 350 | # print the page thing.. | 
|---|
|  | 351 | if ($rowCount > $RESULTS_PER_PAGE) { | 
|---|
|  | 352 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE); | 
|---|
|  | 353 | print qq(<div class="center"> Page: ); | 
|---|
|  | 354 | for (my $i = 1; $i <= $pages; $i++) { | 
|---|
|  | 355 | if ($i == $pageNo) { | 
|---|
|  | 356 | print "<b>$i </b>\n"; | 
|---|
|  | 357 | } else { | 
|---|
| [53] | 358 | print qq(<a href="/ip/cgi-bin/main.cgi?page=$i&input=$webvar{input}&action=search&searchfor=$webvar{searchfor}">$i</a> \n); | 
|---|
| [4] | 359 | } | 
|---|
|  | 360 | } | 
|---|
|  | 361 | print "</div>"; | 
|---|
|  | 362 | } | 
|---|
|  | 363 | } # queryResults | 
|---|
|  | 364 |  | 
|---|
|  | 365 |  | 
|---|
|  | 366 | # Prints table headings.  Accepts any number of arguments; | 
|---|
|  | 367 | # each argument is a table heading. | 
|---|
|  | 368 | sub startTable { | 
|---|
|  | 369 | print qq(<center><table width="98%" cellspacing="0" class="center"><tr>); | 
|---|
|  | 370 |  | 
|---|
|  | 371 | foreach(@_) { | 
|---|
|  | 372 | print qq(<td class="heading">$_</td>); | 
|---|
|  | 373 | } | 
|---|
|  | 374 | print "</tr>\n"; | 
|---|
|  | 375 | } # startTable | 
|---|
|  | 376 |  | 
|---|
|  | 377 |  | 
|---|
|  | 378 | # Return first element of passed SQL query | 
|---|
|  | 379 | sub countRows($) { | 
|---|
|  | 380 | my $sth = $ip_dbh->prepare($_[0]); | 
|---|
|  | 381 | $sth->execute(); | 
|---|
|  | 382 | my @a = $sth->fetchrow_array(); | 
|---|
|  | 383 | $sth->finish(); | 
|---|
|  | 384 | return $a[0]; | 
|---|
|  | 385 | } | 
|---|
|  | 386 |  | 
|---|
|  | 387 |  | 
|---|
|  | 388 | # Initial display:  Show master blocks with total allocated subnets, total free subnets | 
|---|
| [125] | 389 | sub showSummary { | 
|---|
|  | 390 | # this is horrible-ugly-bad and will Go Away real soon now(TM) | 
|---|
| [4] | 391 | print "Content-type: text/html\n\n"; | 
|---|
|  | 392 |  | 
|---|
|  | 393 | startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks', | 
|---|
|  | 394 | 'Free netblocks', 'Largest free block'); | 
|---|
|  | 395 |  | 
|---|
| [125] | 396 | my %allocated; | 
|---|
|  | 397 | my %free; | 
|---|
|  | 398 | my %routed; | 
|---|
|  | 399 | my %bigfree; | 
|---|
|  | 400 |  | 
|---|
|  | 401 | # Count the allocations. | 
|---|
|  | 402 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?"); | 
|---|
|  | 403 | foreach my $master (@masterblocks) { | 
|---|
|  | 404 | $sth->execute("$master"); | 
|---|
|  | 405 | $sth->bind_columns(\$allocated{"$master"}); | 
|---|
|  | 406 | $sth->fetch(); | 
|---|
| [4] | 407 | } | 
|---|
|  | 408 |  | 
|---|
| [125] | 409 | # Count routed blocks | 
|---|
|  | 410 | $sth = $ip_dbh->prepare("select count(*) from routed where cidr <<= ?"); | 
|---|
|  | 411 | foreach my $master (@masterblocks) { | 
|---|
|  | 412 | $sth->execute("$master"); | 
|---|
|  | 413 | $sth->bind_columns(\$routed{"$master"}); | 
|---|
|  | 414 | $sth->fetch(); | 
|---|
| [4] | 415 | } | 
|---|
|  | 416 |  | 
|---|
| [125] | 417 | # Count the free blocks. | 
|---|
| [194] | 418 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ". | 
|---|
|  | 419 | "(routed='y' or routed='n')"); | 
|---|
| [125] | 420 | foreach my $master (@masterblocks) { | 
|---|
|  | 421 | $sth->execute("$master"); | 
|---|
|  | 422 | $sth->bind_columns(\$free{"$master"}); | 
|---|
|  | 423 | $sth->fetch(); | 
|---|
| [4] | 424 | } | 
|---|
|  | 425 |  | 
|---|
| [125] | 426 | # Find the largest free block in each master | 
|---|
| [194] | 427 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ". | 
|---|
|  | 428 | "(routed='y' or routed='n') order by maskbits limit 1"); | 
|---|
| [125] | 429 | foreach my $master (@masterblocks) { | 
|---|
|  | 430 | $sth->execute("$master"); | 
|---|
|  | 431 | $sth->bind_columns(\$bigfree{"$master"}); | 
|---|
|  | 432 | $sth->fetch(); | 
|---|
|  | 433 | } | 
|---|
|  | 434 |  | 
|---|
|  | 435 | # Print the data. | 
|---|
| [4] | 436 | my $count=0; | 
|---|
|  | 437 | foreach my $master (@masterblocks) { | 
|---|
|  | 438 | my @row = ("<a href=\"/ip/cgi-bin/main.cgi?action=showmaster&block=$master\">$master</a>", | 
|---|
|  | 439 | $routed{"$master"}, $allocated{"$master"}, $free{"$master"}, | 
|---|
| [160] | 440 | ( ($bigfree{"$master"} eq '') ? ("<NONE>") : ("/".$bigfree{"$master"}) ) | 
|---|
| [4] | 441 | ); | 
|---|
|  | 442 |  | 
|---|
|  | 443 | printRow(\@row, 'color1' ) if($count%2==0); | 
|---|
|  | 444 | printRow(\@row, 'color2' ) if($count%2!=0); | 
|---|
|  | 445 | $count++; | 
|---|
|  | 446 | } | 
|---|
|  | 447 | print "</table>\n"; | 
|---|
|  | 448 | print qq(<a href="/ip/addmaster.shtml">Add new master block</a><br><br>\n); | 
|---|
|  | 449 | print "Note:  Free blocks noted here include both routed and unrouted blocks.\n"; | 
|---|
|  | 450 |  | 
|---|
|  | 451 | } # showSummary | 
|---|
|  | 452 |  | 
|---|
|  | 453 |  | 
|---|
|  | 454 | # Display detail on master | 
|---|
|  | 455 | # Alrighty then!  We're showing routed blocks within a single master this time. | 
|---|
|  | 456 | # We should be able to steal code from showSummary(), and if I'm really smart | 
|---|
|  | 457 | # I'll figger a way to munge the two together.  (Once I've done that, everything | 
|---|
|  | 458 | # else should follow.  YMMV.) | 
|---|
|  | 459 | sub showMaster { | 
|---|
|  | 460 | printHeader(''); | 
|---|
|  | 461 |  | 
|---|
|  | 462 | print qq(<center><div class="heading">Summarizing routed blocks for ). | 
|---|
|  | 463 | qq($webvar{block}:</div></center><br>\n); | 
|---|
|  | 464 |  | 
|---|
| [125] | 465 | my %allocated; | 
|---|
|  | 466 | my %free; | 
|---|
|  | 467 | my %routed; | 
|---|
|  | 468 | my %bigfree; | 
|---|
|  | 469 |  | 
|---|
| [4] | 470 | my $master = new NetAddr::IP $webvar{block}; | 
|---|
|  | 471 | my @localmasters; | 
|---|
|  | 472 |  | 
|---|
| [125] | 473 | # Fetch only the blocks relevant to this master | 
|---|
| [159] | 474 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr <<= '$master' order by cidr"); | 
|---|
| [4] | 475 | $sth->execute(); | 
|---|
|  | 476 |  | 
|---|
|  | 477 | my $i=0; | 
|---|
|  | 478 | while (my @data = $sth->fetchrow_array()) { | 
|---|
|  | 479 | my $cidr = new NetAddr::IP $data[0]; | 
|---|
| [125] | 480 | $localmasters[$i++] = $cidr; | 
|---|
|  | 481 | $free{"$cidr"} = 0; | 
|---|
|  | 482 | $allocated{"$cidr"} = 0; | 
|---|
|  | 483 | $bigfree{"$cidr"} = 128; | 
|---|
| [4] | 484 | # Retain the routing destination | 
|---|
| [159] | 485 | $routed{"$cidr"} = $data[1]; | 
|---|
| [4] | 486 | } | 
|---|
|  | 487 |  | 
|---|
| [125] | 488 | # Check if there were actually any blocks routed from this master | 
|---|
| [4] | 489 | if ($i > 0) { | 
|---|
|  | 490 | startTable('Routed block','Routed to','Allocated blocks', | 
|---|
|  | 491 | 'Free blocks','Largest free block'); | 
|---|
|  | 492 |  | 
|---|
| [125] | 493 | # Count the allocations | 
|---|
|  | 494 | $sth = $ip_dbh->prepare("select count(*) from allocations where cidr <<= ?"); | 
|---|
|  | 495 | foreach my $master (@localmasters) { | 
|---|
|  | 496 | $sth->execute("$master"); | 
|---|
|  | 497 | $sth->bind_columns(\$allocated{"$master"}); | 
|---|
|  | 498 | $sth->fetch(); | 
|---|
| [4] | 499 | } | 
|---|
|  | 500 |  | 
|---|
| [125] | 501 | # Count the free blocks. | 
|---|
| [194] | 502 | $sth = $ip_dbh->prepare("select count(*) from freeblocks where cidr <<= ? and ". | 
|---|
|  | 503 | "(routed='y' or routed='n')"); | 
|---|
| [125] | 504 | foreach my $master (@localmasters) { | 
|---|
|  | 505 | $sth->execute("$master"); | 
|---|
|  | 506 | $sth->bind_columns(\$free{"$master"}); | 
|---|
|  | 507 | $sth->fetch(); | 
|---|
| [4] | 508 | } | 
|---|
|  | 509 |  | 
|---|
| [125] | 510 | # Get the size of the largest free block | 
|---|
| [194] | 511 | $sth = $ip_dbh->prepare("select maskbits from freeblocks where cidr <<= ? and ". | 
|---|
|  | 512 | "(routed='y' or routed='n') order by maskbits limit 1"); | 
|---|
| [125] | 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; | 
|---|
| [125] | 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]; | 
|---|
| [125] | 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 |  | 
|---|
| [159] | 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 ). | 
|---|
| [159] | 584 | qq($master ($data[0]):</div></center><br>\n); | 
|---|
| [4] | 585 |  | 
|---|
| [125] | 586 | startTable('CIDR allocation','Customer Location','Type','CustID','Description/Name'); | 
|---|
|  | 587 |  | 
|---|
|  | 588 | # Snag the allocations for this block | 
|---|
| [159] | 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()) { | 
|---|
| [159] | 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. | 
|---|
| [159] | 599 | #    $data[2] =~ s/\s+//g; | 
|---|
| [4] | 600 |  | 
|---|
| [192] | 601 | # Prefix subblocks with "Sub " | 
|---|
|  | 602 | my @row = ( (($data[2] =~ /^.r$/) ? 'Sub ' : ''). | 
|---|
|  | 603 | qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), | 
|---|
| [159] | 604 | $data[1], $disp_alloctypes{$data[2]}, $data[3], $data[4]); | 
|---|
| [4] | 605 | # If the allocation is a pool, allow listing of the IPs in the pool. | 
|---|
| [159] | 606 | if ($data[2] =~ /^.[pd]$/) { | 
|---|
| [4] | 607 | $row[0] .= '   <a href="/ip/cgi-bin/main.cgi?action=listpool'. | 
|---|
|  | 608 | "&pool=$data[0]\">List IPs</a>"; | 
|---|
|  | 609 | } | 
|---|
|  | 610 |  | 
|---|
|  | 611 | printRow(\@row, 'color1') if ($count%2 == 0); | 
|---|
|  | 612 | printRow(\@row, 'color2') if ($count%2 != 0); | 
|---|
|  | 613 | $count++; | 
|---|
|  | 614 | } | 
|---|
|  | 615 |  | 
|---|
|  | 616 | print "</table>\n"; | 
|---|
|  | 617 |  | 
|---|
|  | 618 | # If the routed block has no allocations, by definition it only has | 
|---|
|  | 619 | # one free block, and therefore may be deleted. | 
|---|
|  | 620 | if ($count == 0) { | 
|---|
|  | 621 | print qq(<hr width="60%"><center><div class="heading">No allocations in ). | 
|---|
|  | 622 | qq($master.</div></center>\n). | 
|---|
|  | 623 | qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). | 
|---|
|  | 624 | qq(<input type=hidden name=action value="delete">\n). | 
|---|
|  | 625 | qq(<input type=hidden name=block value="$master">\n). | 
|---|
| [192] | 626 | qq(<input type=hidden name=alloctype value="rm">\n). | 
|---|
| [4] | 627 | qq(<input type=submit value=" Remove this block ">\n). | 
|---|
|  | 628 | qq(</form>\n); | 
|---|
|  | 629 | } | 
|---|
|  | 630 |  | 
|---|
|  | 631 | print qq(<hr width="60%">\n<center><div class="heading">Free blocks within routed ). | 
|---|
|  | 632 | qq(submaster $master</div></center>\n); | 
|---|
|  | 633 |  | 
|---|
|  | 634 | startTable('CIDR block','Range'); | 
|---|
|  | 635 |  | 
|---|
|  | 636 | # Snag the free blocks.  We don't really *need* to be pedantic about avoiding | 
|---|
|  | 637 | # unrouted free blocks, but it's better to let the database do the work if we can. | 
|---|
|  | 638 | $count = 0; | 
|---|
| [192] | 639 | $sth = $ip_dbh->prepare("select cidr,routed from freeblocks where cidr <<= '$master'". | 
|---|
|  | 640 | " order by cidr"); | 
|---|
| [4] | 641 | $sth->execute(); | 
|---|
|  | 642 | while (my @data = $sth->fetchrow_array()) { | 
|---|
| [192] | 643 | # cidr,routed | 
|---|
| [4] | 644 | my $cidr = new NetAddr::IP $data[0]; | 
|---|
| [192] | 645 | # Include some HairyPerl(TM) to prefix subblocks with "Sub " | 
|---|
|  | 646 | my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : ''). | 
|---|
|  | 647 | qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>), | 
|---|
| [24] | 648 | $cidr->range); | 
|---|
| [125] | 649 | printRow(\@row, 'color1') if ($count%2 == 0); | 
|---|
|  | 650 | printRow(\@row, 'color2') if ($count%2 != 0); | 
|---|
|  | 651 | $count++; | 
|---|
| [4] | 652 | } | 
|---|
|  | 653 |  | 
|---|
|  | 654 | print "</table>\n"; | 
|---|
|  | 655 | } # showRBlock | 
|---|
|  | 656 |  | 
|---|
|  | 657 |  | 
|---|
|  | 658 | # List the IPs used in a pool | 
|---|
|  | 659 | sub listPool { | 
|---|
|  | 660 | printHeader(''); | 
|---|
|  | 661 |  | 
|---|
|  | 662 | my $cidr = new NetAddr::IP $webvar{pool}; | 
|---|
|  | 663 |  | 
|---|
| [159] | 664 | my ($pooltype,$poolcity); | 
|---|
|  | 665 |  | 
|---|
| [4] | 666 | # Snag pool info for heading | 
|---|
| [159] | 667 | $sth = $ip_dbh->prepare("select type,city from allocations where cidr='$cidr'"); | 
|---|
| [4] | 668 | $sth->execute; | 
|---|
| [159] | 669 | $sth->bind_columns(\$pooltype, \$poolcity); | 
|---|
|  | 670 | $sth->fetch() || carp $sth->errstr; | 
|---|
| [4] | 671 |  | 
|---|
|  | 672 | print qq(<center><div class="heading">Listing pool IPs for $cidr<br>\n). | 
|---|
| [159] | 673 | qq(($disp_alloctypes{$pooltype} in $poolcity)</div></center><br>\n); | 
|---|
|  | 674 | # Only display net/gw/bcast if it's a "real" netblock and not a PPP(oE) lunacy | 
|---|
|  | 675 | if ($pooltype =~ /^.d$/) { | 
|---|
|  | 676 | print qq(<div class="indent"><b>Reserved IPs:</b><br>\n); | 
|---|
|  | 677 | print qq(<div class="indent"><table><tr class=color1><td>Network IP:</td><td>). | 
|---|
| [4] | 678 | $cidr->addr."</td></tr>\n"; | 
|---|
| [159] | 679 | $cidr++; | 
|---|
|  | 680 | print "<tr class=color2><td>Gateway:</td><td>".$cidr->addr."</td></tr>\n"; | 
|---|
|  | 681 | $cidr--;  $cidr--; | 
|---|
|  | 682 | print "<tr class=color1><td>Broadcast:</td><td>".$cidr->addr."</td></tr>\n". | 
|---|
| [4] | 683 | "<tr><td>Netmask:</td><td>".$cidr->mask."</td></tr>\n". | 
|---|
|  | 684 | "</table></div></div>\n"; | 
|---|
| [159] | 685 | } | 
|---|
| [4] | 686 |  | 
|---|
|  | 687 | # probably have to add an "edit IP allocation" link here somewhere. | 
|---|
|  | 688 |  | 
|---|
|  | 689 | startTable('IP','Customer ID','Available?','Description',''); | 
|---|
| [159] | 690 | $sth = $ip_dbh->prepare("select ip,custid,available,description,type". | 
|---|
|  | 691 | " from poolips where pool='$webvar{pool}' order by ip"); | 
|---|
| [4] | 692 | $sth->execute; | 
|---|
|  | 693 | my $count = 0; | 
|---|
|  | 694 | while (my @data = $sth->fetchrow_array) { | 
|---|
| [75] | 695 | # pool,ip,custid,city,ptype,available,notes,description,circuitid | 
|---|
| [159] | 696 | # ip,custid,available,description,type | 
|---|
|  | 697 | # If desc is "null", make it not null.  <g> | 
|---|
|  | 698 | if ($data[3] eq '') { | 
|---|
|  | 699 | $data[3] = ' '; | 
|---|
| [4] | 700 | } | 
|---|
|  | 701 | # Some nice hairy Perl to decide whether to allow unassigning each IP | 
|---|
| [159] | 702 | #   -> if $data[2] (aka poolips.available) == 'n' then we print the unassign link | 
|---|
| [4] | 703 | #      else we print a blank space | 
|---|
| [159] | 704 | my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), | 
|---|
|  | 705 | $data[1],$data[2],$data[3], | 
|---|
|  | 706 | ( ($data[2] eq 'n') ? | 
|---|
|  | 707 | ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&". | 
|---|
|  | 708 | "alloctype=$data[4]\">Unassign this IP</a>") : | 
|---|
| [4] | 709 | (" ") ) | 
|---|
|  | 710 | ); | 
|---|
|  | 711 | printRow(\@row, 'color1') if($count%2==0); | 
|---|
|  | 712 | printRow(\@row, 'color2') if($count%2!=0); | 
|---|
|  | 713 | $count++; | 
|---|
|  | 714 | } | 
|---|
|  | 715 | print "</table>\n"; | 
|---|
|  | 716 |  | 
|---|
|  | 717 | } # end listPool | 
|---|
|  | 718 |  | 
|---|
|  | 719 |  | 
|---|
| [125] | 720 | # Show "Add new allocation" page.  Note that the actual page may | 
|---|
|  | 721 | # be one of two templates, and the lists come from the database. | 
|---|
| [4] | 722 | sub assignBlock { | 
|---|
|  | 723 | printHeader(''); | 
|---|
|  | 724 |  | 
|---|
| [24] | 725 | my $html; | 
|---|
|  | 726 |  | 
|---|
|  | 727 | # New special case- block to assign is specified | 
|---|
|  | 728 | if ($webvar{block} ne '') { | 
|---|
|  | 729 | open HTML, "../fb-assign.html" | 
|---|
|  | 730 | or croak "Could not open fb-assign.html: $!"; | 
|---|
|  | 731 | $html = join('',<HTML>); | 
|---|
|  | 732 | close HTML; | 
|---|
|  | 733 | my $block = new NetAddr::IP $webvar{block}; | 
|---|
|  | 734 | $html =~ s|\$\$BLOCK\$\$|$block|g; | 
|---|
|  | 735 | $html =~ s|\$\$MASKBITS\$\$|$block->masklen|; | 
|---|
| [98] | 736 | my $typelist = ''; | 
|---|
| [192] | 737 |  | 
|---|
|  | 738 | # This is a little dangerous, as it's *theoretically* possible to | 
|---|
|  | 739 | # get fbtype='n' (aka a non-routed freeblock).  However, should | 
|---|
|  | 740 | # someone manage to get there, they get what they deserve. | 
|---|
|  | 741 | if ($webvar{fbtype} ne 'y') { | 
|---|
|  | 742 | # Snag the type of the block from the database.  We have no | 
|---|
|  | 743 | # convenient way to pass this in from the calling location.  :/ | 
|---|
|  | 744 | $sth = $ip_dbh->prepare("select type from allocations where cidr >>='$block'"); | 
|---|
|  | 745 | $sth->execute; | 
|---|
|  | 746 | my @data = $sth->fetchrow_array; | 
|---|
|  | 747 | $data[0] =~ s/c$/r/;      # Munge the type into the correct form | 
|---|
|  | 748 | $typelist = "$list_alloctypes{$data[0]}<input type=hidden name=alloctype value=$data[0]>\n"; | 
|---|
|  | 749 | } else { | 
|---|
|  | 750 | $typelist .= qq(<select name="alloctype">\n); | 
|---|
|  | 751 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 500 ". | 
|---|
|  | 752 | "and type not like '_i' and type not like '_r' order by listorder"); | 
|---|
|  | 753 | $sth->execute; | 
|---|
|  | 754 | my @data = $sth->fetchrow_array; | 
|---|
|  | 755 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n"; | 
|---|
|  | 756 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 757 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n"; | 
|---|
|  | 758 | } | 
|---|
|  | 759 | $typelist .= "</select>\n"; | 
|---|
| [98] | 760 | } | 
|---|
|  | 761 | $html =~ s|\$\$TYPELIST\$\$|$typelist|g; | 
|---|
| [24] | 762 | } else { | 
|---|
|  | 763 | open HTML, "../assign.html" | 
|---|
| [4] | 764 | or croak "Could not open assign.html: $!"; | 
|---|
| [24] | 765 | $html = join('',<HTML>); | 
|---|
| [91] | 766 | close HTML; | 
|---|
| [32] | 767 | my $masterlist = "<select name=allocfrom><option selected>-</option>\n"; | 
|---|
|  | 768 | foreach my $master (@masterblocks) { | 
|---|
|  | 769 | $masterlist .= "<option>$master</option>\n"; | 
|---|
|  | 770 | } | 
|---|
|  | 771 | $masterlist .= "</select>\n"; | 
|---|
|  | 772 | $html =~ s|\$\$MASTERLIST\$\$|$masterlist|g; | 
|---|
| [91] | 773 | my $pops = ''; | 
|---|
|  | 774 | foreach my $pop (@poplist) { | 
|---|
|  | 775 | $pops .= "<option>$pop</option>\n"; | 
|---|
|  | 776 | } | 
|---|
|  | 777 | $html =~ s|\$\$POPLIST\$\$|$pops|g; | 
|---|
| [98] | 778 | my $typelist = ''; | 
|---|
|  | 779 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder"); | 
|---|
|  | 780 | $sth->execute; | 
|---|
|  | 781 | my @data = $sth->fetchrow_array; | 
|---|
|  | 782 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n"; | 
|---|
|  | 783 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 784 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n"; | 
|---|
|  | 785 | } | 
|---|
|  | 786 | $html =~ s|\$\$TYPELIST\$\$|$typelist|g; | 
|---|
| [24] | 787 | } | 
|---|
| [91] | 788 | my $cities = ''; | 
|---|
|  | 789 | foreach my $city (@citylist) { | 
|---|
|  | 790 | $cities .= "<option>$city</option>\n"; | 
|---|
|  | 791 | } | 
|---|
|  | 792 | $html =~ s|\$\$ALLCITIES\$\$|$cities|g; | 
|---|
| [4] | 793 |  | 
|---|
|  | 794 | print $html; | 
|---|
|  | 795 |  | 
|---|
|  | 796 | } # assignBlock | 
|---|
|  | 797 |  | 
|---|
|  | 798 |  | 
|---|
|  | 799 | # Take info on requested IP assignment and see what we can provide. | 
|---|
|  | 800 | sub confirmAssign { | 
|---|
|  | 801 | printHeader(''); | 
|---|
|  | 802 |  | 
|---|
|  | 803 | my $cidr; | 
|---|
|  | 804 | my $alloc_from; | 
|---|
|  | 805 |  | 
|---|
|  | 806 | # Going to manually validate some items. | 
|---|
|  | 807 | # custid and city are automagic. | 
|---|
| [125] | 808 | return if !validateInput(); | 
|---|
| [4] | 809 |  | 
|---|
|  | 810 | # Several different cases here. | 
|---|
|  | 811 | # Static IP vs netblock | 
|---|
|  | 812 | #  + Different flavours of static IP | 
|---|
|  | 813 | #  + Different flavours of netblock | 
|---|
|  | 814 |  | 
|---|
| [159] | 815 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| [4] | 816 | my ($base,undef) = split //, $webvar{alloctype};    # split into individual chars | 
|---|
|  | 817 | my $sql; | 
|---|
|  | 818 | # Check for pools in Subury or North Bay if DSL or server pool.  Anywhere else is | 
|---|
|  | 819 | # invalid and shouldn't be in the db in the first place. | 
|---|
|  | 820 | # ... aside from #^%#$%#@#^%^^!!!! legacy data.  GRRR. | 
|---|
|  | 821 | # Note that we want to retain the requested city to relate to customer info. | 
|---|
| [159] | 822 | ##fixme This needs thought. | 
|---|
|  | 823 | ##SELECT DISTINCT pool, Count(*) FROM poolips where available='y' GROUP BY pool; | 
|---|
| [4] | 824 | if ($base =~ /^[ds]$/) { | 
|---|
|  | 825 | $sql = "select * from poolips where available='y' and". | 
|---|
| [164] | 826 | " type='$webvar{alloctype}' and (city='Sudbury' or city='North Bay')"; | 
|---|
| [4] | 827 | } else { | 
|---|
|  | 828 | $sql = "select * from poolips where available='y' and". | 
|---|
| [164] | 829 | " type='$webvar{alloctype}' and city='$webvar{pop}'"; | 
|---|
| [4] | 830 | } | 
|---|
|  | 831 |  | 
|---|
|  | 832 | # Now that we know where we're looking, we can list the pools with free IPs. | 
|---|
|  | 833 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 834 | $sth->execute; | 
|---|
|  | 835 | my %ipcount; | 
|---|
|  | 836 | my $optionlist; | 
|---|
|  | 837 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 838 | $ipcount{$data[0]}++; | 
|---|
|  | 839 | } | 
|---|
| [86] | 840 | $sth = $ip_dbh->prepare("select city from allocations where cidr=?"); | 
|---|
| [4] | 841 | foreach my $key (keys %ipcount) { | 
|---|
| [86] | 842 | $sth->execute($key); | 
|---|
|  | 843 | my @data = $sth->fetchrow_array; | 
|---|
|  | 844 | $optionlist .= "<option value='$key'>$key [$ipcount{$key} free IP(s)] in $data[0]</option>\n"; | 
|---|
| [4] | 845 | } | 
|---|
|  | 846 | $cidr = "Single static IP"; | 
|---|
|  | 847 | $alloc_from = "<select name=alloc_from>".$optionlist."</select>\n"; | 
|---|
|  | 848 |  | 
|---|
|  | 849 | } else { # end show pool options | 
|---|
| [24] | 850 |  | 
|---|
|  | 851 | if ($webvar{fbassign} eq 'y') { | 
|---|
|  | 852 | $cidr = new NetAddr::IP $webvar{block}; | 
|---|
|  | 853 | $webvar{maskbits} = $cidr->masklen; | 
|---|
|  | 854 | } else { # done with direct freeblocks assignment | 
|---|
|  | 855 |  | 
|---|
|  | 856 | if (!$webvar{maskbits}) { | 
|---|
| [125] | 857 | printError("Please specify a CIDR mask length."); | 
|---|
|  | 858 | return; | 
|---|
| [24] | 859 | } | 
|---|
|  | 860 | my $sql; | 
|---|
|  | 861 | my $city; | 
|---|
|  | 862 | my $failmsg; | 
|---|
| [192] | 863 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [32] | 864 | if ($webvar{allocfrom} ne '-') { | 
|---|
|  | 865 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'". | 
|---|
|  | 866 | " and cidr <<= '$webvar{allocfrom}' order by maskbits desc"; | 
|---|
|  | 867 | } else { | 
|---|
|  | 868 | $sql = "select * from freeblocks where maskbits<=$webvar{maskbits} and routed='n'". | 
|---|
|  | 869 | " order by maskbits desc"; | 
|---|
|  | 870 | } | 
|---|
| [24] | 871 | $failmsg = "No suitable free block found.<br>\nWe do not have a free". | 
|---|
|  | 872 | " routeable block of that size.<br>\nYou will have to either route". | 
|---|
|  | 873 | " a set of smaller netblocks or a single smaller netblock."; | 
|---|
| [4] | 874 | } else { | 
|---|
| [125] | 875 | ##fixme | 
|---|
|  | 876 | # This section needs serious Pondering. | 
|---|
| [192] | 877 | # Pools of all types get assigned to the POP they're "routed from" | 
|---|
|  | 878 | # This includes WAN blocks and other netblock "containers" | 
|---|
|  | 879 | if ($webvar{alloctype} =~ /^.[pdc]$/) { | 
|---|
| [24] | 880 | if (($webvar{city} !~ /^(Sudbury|North Bay)$/) && ($webvar{alloctype} eq 'dp')) { | 
|---|
| [125] | 881 | printError("You must chose Sudbury or North Bay for DSL pools."); | 
|---|
|  | 882 | return; | 
|---|
|  | 883 | } | 
|---|
| [39] | 884 | $city = $webvar{city}; | 
|---|
| [24] | 885 | $failmsg = "No suitable free block found.<br>\nYou will have to route another". | 
|---|
| [125] | 886 | " superblock from one of the<br>\nmaster blocks in Sudbury or chose a smaller". | 
|---|
| [24] | 887 | " block size for the pool."; | 
|---|
|  | 888 | } else { | 
|---|
|  | 889 | $city = $webvar{pop}; | 
|---|
|  | 890 | $failmsg = "No suitable free block found.<br>\nYou will have to route another". | 
|---|
| [101] | 891 | " superblock to $webvar{pop}<br>\nfrom one of the master blocks in Sudbury or". | 
|---|
| [24] | 892 | " chose a smaller blocksize."; | 
|---|
|  | 893 | } | 
|---|
| [32] | 894 | if ($webvar{allocfrom} ne '-') { | 
|---|
| [159] | 895 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}". | 
|---|
| [192] | 896 | " and cidr <<= '$webvar{allocfrom}' and routed='". | 
|---|
|  | 897 | (($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y')."' order by maskbits desc,cidr"; | 
|---|
| [32] | 898 | } else { | 
|---|
| [159] | 899 | $sql = "select cidr from freeblocks where city='$city' and maskbits<=$webvar{maskbits}". | 
|---|
| [192] | 900 | " and routed='".(($webvar{alloctype} =~ /^(.)r$/) ? "$1" : 'y'). | 
|---|
|  | 901 | "' order by maskbits desc,cidr"; | 
|---|
| [32] | 902 | } | 
|---|
| [4] | 903 | } | 
|---|
| [24] | 904 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 905 | $sth->execute; | 
|---|
|  | 906 | my @data = $sth->fetchrow_array(); | 
|---|
|  | 907 | if ($data[0] eq "") { | 
|---|
| [125] | 908 | printError($failmsg); | 
|---|
|  | 909 | return; | 
|---|
| [24] | 910 | } | 
|---|
|  | 911 | $cidr = new NetAddr::IP $data[0]; | 
|---|
|  | 912 | } # check for freeblocks assignment or IPDB-controlled assignment | 
|---|
| [4] | 913 |  | 
|---|
|  | 914 | $alloc_from = qq($cidr<input type=hidden name=alloc_from value="$cidr">); | 
|---|
|  | 915 |  | 
|---|
|  | 916 | # If the block to be allocated is smaller than the one we found, | 
|---|
|  | 917 | # figure out the "real" block to be allocated. | 
|---|
|  | 918 | if ($cidr->masklen() ne $webvar{maskbits}) { | 
|---|
|  | 919 | my $maskbits = $cidr->masklen(); | 
|---|
|  | 920 | my @subblocks; | 
|---|
|  | 921 | while ($maskbits++ < $webvar{maskbits}) { | 
|---|
|  | 922 | @subblocks = $cidr->split($maskbits); | 
|---|
|  | 923 | } | 
|---|
|  | 924 | $cidr = $subblocks[0]; | 
|---|
|  | 925 | } | 
|---|
| [159] | 926 | } # if ($webvar{alloctype} =~ /^.i$/) | 
|---|
| [4] | 927 |  | 
|---|
|  | 928 | open HTML, "../confirm.html" | 
|---|
|  | 929 | or croak "Could not open confirm.html: $!"; | 
|---|
|  | 930 | my $html = join '', <HTML>; | 
|---|
|  | 931 | close HTML; | 
|---|
|  | 932 |  | 
|---|
|  | 933 | ### gotta fix this in final | 
|---|
|  | 934 | # Stick in customer info as necessary - if it's blank, it just ends | 
|---|
|  | 935 | # up as blank lines ignored in the rendering of the page | 
|---|
|  | 936 | my $custbits; | 
|---|
|  | 937 | $html =~ s|\$\$CUSTBITS\$\$|$custbits|g; | 
|---|
|  | 938 | ### | 
|---|
|  | 939 |  | 
|---|
|  | 940 | # Stick in the allocation data | 
|---|
|  | 941 | $html =~ s|\$\$ALLOC_TYPE\$\$|$webvar{alloctype}|g; | 
|---|
| [98] | 942 | $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$webvar{alloctype}}|g; | 
|---|
| [4] | 943 | $html =~ s|\$\$ALLOC_FROM\$\$|$alloc_from|g; | 
|---|
|  | 944 | $html =~ s|\$\$CIDR\$\$|$cidr|g; | 
|---|
| [82] | 945 | $webvar{city} = desanitize($webvar{city}); | 
|---|
| [4] | 946 | $html =~ s|\$\$CITY\$\$|$webvar{city}|g; | 
|---|
|  | 947 | $html =~ s|\$\$CUSTID\$\$|$webvar{custid}|g; | 
|---|
| [83] | 948 | $webvar{circid} = desanitize($webvar{circid}); | 
|---|
| [75] | 949 | $html =~ s|\$\$CIRCID\$\$|$webvar{circid}|g; | 
|---|
| [4] | 950 | $webvar{desc} = desanitize($webvar{desc}); | 
|---|
| [89] | 951 | $html =~ s|\$\$DESC\$\$|$webvar{desc}|g; | 
|---|
| [4] | 952 | $webvar{notes} = desanitize($webvar{notes}); | 
|---|
|  | 953 | $html =~ s|\$\$NOTES\$\$|$webvar{notes}|g; | 
|---|
|  | 954 | $html =~ s|\$\$ACTION\$\$|insert|g; | 
|---|
|  | 955 |  | 
|---|
|  | 956 | print $html; | 
|---|
|  | 957 |  | 
|---|
|  | 958 | } # end confirmAssign | 
|---|
|  | 959 |  | 
|---|
|  | 960 |  | 
|---|
|  | 961 | # Do the work of actually inserting a block in the database. | 
|---|
|  | 962 | sub insertAssign { | 
|---|
|  | 963 | # Some things are done more than once. | 
|---|
|  | 964 | printHeader(''); | 
|---|
| [125] | 965 | return if !validateInput(); | 
|---|
| [4] | 966 |  | 
|---|
| [125] | 967 | # $code is "success" vs "failure", $msg contains OK for a | 
|---|
|  | 968 | # successful netblock allocation, the IP allocated for static | 
|---|
|  | 969 | # IP, or the error message if an error occurred. | 
|---|
|  | 970 | my ($code,$msg) = allocateBlock($ip_dbh, $webvar{fullcidr}, $webvar{alloc_from}, | 
|---|
|  | 971 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
|  | 972 | $webvar{circid}); | 
|---|
| [4] | 973 |  | 
|---|
| [125] | 974 | if ($code eq 'OK') { | 
|---|
|  | 975 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 976 | print qq(<div class="center"><div class="heading">The IP $msg has been allocated to customer $webvar{custid}</div></div>); | 
|---|
| [122] | 977 | # Notify tech@example.com | 
|---|
|  | 978 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
| [125] | 979 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
| [122] | 980 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| [125] | 981 | } else { | 
|---|
|  | 982 | print qq(<div class="center"><div class="heading">The block $webvar{fullcidr} was ). | 
|---|
| [192] | 983 | "sucessfully added as: $disp_alloctypes{$webvar{alloctype}}</div></div>"; | 
|---|
| [122] | 984 | } | 
|---|
| [125] | 985 | syslog "notice", "$authuser allocated '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
|  | 986 | "'$webvar{alloctype}'"; | 
|---|
|  | 987 | } else { | 
|---|
|  | 988 | syslog "err", "Allocation of '$webvar{fullcidr}' to '$webvar{custid}' as ". | 
|---|
|  | 989 | "'$webvar{alloctype}' by $authuser failed: '$msg'"; | 
|---|
| [192] | 990 | printError("Allocation of $webvar{fullcidr} as '$disp_alloctypes{$webvar{alloctype}}'". | 
|---|
| [159] | 991 | " failed:<br>\n$msg\n"); | 
|---|
| [125] | 992 | } | 
|---|
| [122] | 993 |  | 
|---|
| [4] | 994 | } # end insertAssign() | 
|---|
|  | 995 |  | 
|---|
|  | 996 |  | 
|---|
|  | 997 | # Does some basic checks on common input data to make sure nothing | 
|---|
|  | 998 | # *really* weird gets in to the database through this script. | 
|---|
|  | 999 | # Does NOT do complete input validation!!! | 
|---|
|  | 1000 | sub validateInput { | 
|---|
|  | 1001 | if ($webvar{city} eq '-') { | 
|---|
| [125] | 1002 | printError("Please choose a city."); | 
|---|
|  | 1003 | return; | 
|---|
| [4] | 1004 | } | 
|---|
| [140] | 1005 |  | 
|---|
|  | 1006 | # Alloctype check. | 
|---|
| [4] | 1007 | chomp $webvar{alloctype}; | 
|---|
| [140] | 1008 | if (!grep /$webvar{alloctype}/, keys %disp_alloctypes) { | 
|---|
|  | 1009 | # Danger! Danger!  alloctype should ALWAYS be set by a dropdown.  Anyone | 
|---|
|  | 1010 | # managing to call things in such a way as to cause this deserves a cryptic error. | 
|---|
|  | 1011 | printError("Invalid alloctype"); | 
|---|
|  | 1012 | return; | 
|---|
|  | 1013 | } | 
|---|
|  | 1014 |  | 
|---|
|  | 1015 | # CustID check | 
|---|
| [4] | 1016 | # We have different handling for customer allocations and "internal" or "our" allocations | 
|---|
| [140] | 1017 | if ($webvar{alloctype} =~ /^(cn|.i)$/) { | 
|---|
| [4] | 1018 | if (!$webvar{custid}) { | 
|---|
| [125] | 1019 | printError("Please enter a customer ID."); | 
|---|
|  | 1020 | return; | 
|---|
| [4] | 1021 | } | 
|---|
| [116] | 1022 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) { | 
|---|
| [56] | 1023 | # Force uppercase for now... | 
|---|
|  | 1024 | $webvar{custid} =~ tr/a-z/A-Z/; | 
|---|
|  | 1025 | # Crosscheck with ... er...  something. | 
|---|
|  | 1026 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
| [125] | 1027 | if ($CustIDCK::Error) { | 
|---|
|  | 1028 | printError("Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
|  | 1029 | return; | 
|---|
|  | 1030 | } | 
|---|
|  | 1031 | if (!$status) { | 
|---|
|  | 1032 | printError("Customer ID not valid.  Make sure the Customer ID ". | 
|---|
|  | 1033 | "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ". | 
|---|
|  | 1034 | "non-customer assignments."); | 
|---|
|  | 1035 | return; | 
|---|
|  | 1036 | } | 
|---|
| [56] | 1037 | #"Please enter a valid customer ID- this must be a 7- or 10-digit number, or STAFF for | 
|---|
|  | 1038 | #static IPs for staff."); | 
|---|
| [4] | 1039 | } | 
|---|
| [56] | 1040 | #    print "<!-- [ In validateInput().  Insert customer ID cross-check here. ] -->\n"; | 
|---|
| [140] | 1041 | } else { | 
|---|
| [168] | 1042 | # New!  Improved!  And now Loaded From The Database!! | 
|---|
| [56] | 1043 | if ((!$webvar{custid}) || ($webvar{custid} ne 'STAFF')) { | 
|---|
| [168] | 1044 | $webvar{custid} = $def_custids{$webvar{alloctype}}; | 
|---|
| [56] | 1045 | } | 
|---|
| [4] | 1046 | } | 
|---|
| [125] | 1047 |  | 
|---|
|  | 1048 | # Check POP location | 
|---|
|  | 1049 | my $flag; | 
|---|
| [192] | 1050 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [125] | 1051 | $flag = 'for a routed netblock'; | 
|---|
|  | 1052 | foreach (@poplist) { | 
|---|
|  | 1053 | if (/^$webvar{city}$/) { | 
|---|
|  | 1054 | $flag = 'n'; | 
|---|
|  | 1055 | last; | 
|---|
|  | 1056 | } | 
|---|
|  | 1057 | } | 
|---|
|  | 1058 | } else { | 
|---|
|  | 1059 | $flag = 'n'; | 
|---|
|  | 1060 | if ($webvar{pop} =~ /^-$/) { | 
|---|
|  | 1061 | $flag = 'to route the block from/through'; | 
|---|
|  | 1062 | } | 
|---|
|  | 1063 | } | 
|---|
|  | 1064 | if ($flag ne 'n') { | 
|---|
|  | 1065 | printError("Please choose a valid POP location $flag.  Valid ". | 
|---|
|  | 1066 | "POP locations are currently:<br>\n".join (" - ", @poplist)); | 
|---|
|  | 1067 | return; | 
|---|
|  | 1068 | } | 
|---|
|  | 1069 |  | 
|---|
|  | 1070 | return 'OK'; | 
|---|
| [4] | 1071 | } # end validateInput | 
|---|
|  | 1072 |  | 
|---|
|  | 1073 |  | 
|---|
|  | 1074 | # Displays details of a specific allocation in a form | 
|---|
|  | 1075 | # Allows update/delete | 
|---|
|  | 1076 | # action=edit | 
|---|
|  | 1077 | sub edit { | 
|---|
|  | 1078 | printHeader(''); | 
|---|
|  | 1079 |  | 
|---|
|  | 1080 | my $sql; | 
|---|
|  | 1081 |  | 
|---|
|  | 1082 | # Two cases:  block is a netblock, or block is a static IP from a pool | 
|---|
|  | 1083 | # because I'm lazy, we'll try to make the SELECT's bring out identical)ish) data | 
|---|
|  | 1084 | if ($webvar{block} =~ /\/32$/) { | 
|---|
| [162] | 1085 | $sql = "select ip,custid,type,city,circuitid,description,notes from poolips where ip='$webvar{block}'"; | 
|---|
| [4] | 1086 | } else { | 
|---|
| [75] | 1087 | $sql = "select cidr,custid,type,city,circuitid,description,notes from allocations where cidr='$webvar{block}'" | 
|---|
| [4] | 1088 | } | 
|---|
|  | 1089 |  | 
|---|
|  | 1090 | # gotta snag block info from db | 
|---|
|  | 1091 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 1092 | $sth->execute; | 
|---|
|  | 1093 | my @data = $sth->fetchrow_array; | 
|---|
|  | 1094 |  | 
|---|
|  | 1095 | # Clean up extra whitespace on alloc type | 
|---|
|  | 1096 | $data[2] =~ s/\s//; | 
|---|
|  | 1097 |  | 
|---|
| [159] | 1098 | ##fixme LEGACY CODE | 
|---|
| [4] | 1099 | # Postfix "i" on pool IP types | 
|---|
| [97] | 1100 | if ($data[2] =~ /^[cdsmw]$/) { | 
|---|
| [4] | 1101 | $data[2] .= "i"; | 
|---|
|  | 1102 | } | 
|---|
|  | 1103 |  | 
|---|
|  | 1104 | open (HTML, "../editDisplay.html") | 
|---|
|  | 1105 | or croak "Could not open editDisplay.html :$!"; | 
|---|
|  | 1106 | my $html = join('', <HTML>); | 
|---|
|  | 1107 |  | 
|---|
|  | 1108 | # We can't let the city be changed here;  this block is a part of | 
|---|
|  | 1109 | # a larger routed allocation and therefore by definition can't be moved. | 
|---|
|  | 1110 | # block and city are static. | 
|---|
|  | 1111 | ##fixme | 
|---|
|  | 1112 | # Needs thinking.  Have to allow changes to city to correct errors, no? | 
|---|
|  | 1113 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g; | 
|---|
|  | 1114 | $html =~ s/\$\$CITY\$\$/$data[3]/g; | 
|---|
|  | 1115 |  | 
|---|
|  | 1116 | # Screw it.  Changing allocation types gets very ugly VERY quickly- especially | 
|---|
|  | 1117 | # with the much longer list of allocation types. | 
|---|
|  | 1118 | # We'll just show what type of block it is. | 
|---|
|  | 1119 |  | 
|---|
| [35] | 1120 | # this has now been Requested, so here goes. | 
|---|
| [4] | 1121 |  | 
|---|
| [192] | 1122 | ##fixme The check here should be built from the database | 
|---|
|  | 1123 | if ($data[2] =~ /^.[ne]$/) { | 
|---|
| [35] | 1124 | # Block that can be changed | 
|---|
|  | 1125 | my $blockoptions = "<select name=alloctype><option". | 
|---|
| [192] | 1126 | (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". | 
|---|
|  | 1127 | (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".  (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option". | 
|---|
|  | 1128 | (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".        (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option". | 
|---|
|  | 1129 | (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic wireless netblock</option>\n<option".     (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option". | 
|---|
| [35] | 1130 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option". | 
|---|
| [192] | 1131 | (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option". | 
|---|
| [133] | 1132 | (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". | 
|---|
| [35] | 1133 | "</select>\n"; | 
|---|
|  | 1134 | $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g; | 
|---|
|  | 1135 | } else { | 
|---|
| [98] | 1136 | $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g; | 
|---|
| [35] | 1137 | } | 
|---|
|  | 1138 |  | 
|---|
| [4] | 1139 | # These can be modified, although CustID changes may get ignored. | 
|---|
|  | 1140 | $html =~ s/\$\$CUSTID\$\$/$data[1]/g; | 
|---|
| [75] | 1141 | $html =~ s/\$\$TYPE\$\$/$data[2]/g; | 
|---|
|  | 1142 | $html =~ s/\$\$CIRCID\$\$/$data[4]/g; | 
|---|
|  | 1143 | $html =~ s/\$\$DESC\$\$/$data[5]/g; | 
|---|
|  | 1144 | $html =~ s/\$\$NOTES\$\$/$data[6]/g; | 
|---|
| [4] | 1145 |  | 
|---|
|  | 1146 | print $html; | 
|---|
|  | 1147 |  | 
|---|
|  | 1148 | } # edit() | 
|---|
|  | 1149 |  | 
|---|
|  | 1150 |  | 
|---|
|  | 1151 | # Stuff new info about a block into the db | 
|---|
|  | 1152 | # action=update | 
|---|
|  | 1153 | sub update { | 
|---|
|  | 1154 | printHeader(''); | 
|---|
|  | 1155 |  | 
|---|
|  | 1156 | # Make sure incoming data is in correct format - custID among other things. | 
|---|
|  | 1157 | validateInput; | 
|---|
|  | 1158 |  | 
|---|
|  | 1159 | # SQL transaction wrapper | 
|---|
|  | 1160 | eval { | 
|---|
|  | 1161 | # Relatively simple SQL transaction here. | 
|---|
|  | 1162 | my $sql; | 
|---|
| [159] | 1163 | if (my $pooltype = ($webvar{alloctype} =~ /^(.)i$/) ) { | 
|---|
| [75] | 1164 | $sql = "update poolips set custid='$webvar{custid}',notes='$webvar{notes}',". | 
|---|
|  | 1165 | "circuitid='$webvar{circid}',description='$webvar{desc}' ". | 
|---|
| [4] | 1166 | "where ip='$webvar{block}'"; | 
|---|
|  | 1167 | } else { | 
|---|
|  | 1168 | $sql = "update allocations set custid='$webvar{custid}',". | 
|---|
| [35] | 1169 | "description='$webvar{desc}',notes='$webvar{notes}',city='$webvar{city}',". | 
|---|
| [75] | 1170 | "type='$webvar{alloctype}',circuitid='$webvar{circid}' where cidr='$webvar{block}'"; | 
|---|
| [4] | 1171 | } | 
|---|
| [159] | 1172 | # Log the details of the change. | 
|---|
|  | 1173 | syslog "debug", $sql; | 
|---|
| [4] | 1174 | $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 1175 | $sth->execute; | 
|---|
|  | 1176 | $ip_dbh->commit; | 
|---|
|  | 1177 | }; | 
|---|
|  | 1178 | if ($@) { | 
|---|
| [163] | 1179 | my $msg = $@; | 
|---|
|  | 1180 | carp "Transaction aborted because $msg"; | 
|---|
| [4] | 1181 | eval { $ip_dbh->rollback; }; | 
|---|
| [163] | 1182 | syslog "err", "$authuser could not update block/IP '$webvar{block}': '$msg'"; | 
|---|
|  | 1183 | printError("Could not update block/IP $webvar{block}: $msg"); | 
|---|
| [125] | 1184 | return; | 
|---|
| [4] | 1185 | } | 
|---|
|  | 1186 |  | 
|---|
|  | 1187 | # If we get here, the operation succeeded. | 
|---|
|  | 1188 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
|  | 1189 | open (HTML, "../updated.html") | 
|---|
| [125] | 1190 | or croak "Could not open updated.html :$!"; | 
|---|
| [4] | 1191 | my $html = join('', <HTML>); | 
|---|
|  | 1192 |  | 
|---|
|  | 1193 | $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g; | 
|---|
| [89] | 1194 | $webvar{city} = desanitize($webvar{city}); | 
|---|
| [4] | 1195 | $html =~ s/\$\$CITY\$\$/$webvar{city}/g; | 
|---|
|  | 1196 | $html =~ s/\$\$ALLOCTYPE\$\$/$webvar{alloctype}/g; | 
|---|
| [98] | 1197 | $html =~ s/\$\$TYPEFULL\$\$/$disp_alloctypes{$webvar{alloctype}}/g; | 
|---|
| [4] | 1198 | $html =~ s/\$\$CUSTID\$\$/$webvar{custid}/g; | 
|---|
| [89] | 1199 | $webvar{circid} = desanitize($webvar{circid}); | 
|---|
| [75] | 1200 | $html =~ s/\$\$CIRCID\$\$/$webvar{circid}/g; | 
|---|
| [89] | 1201 | $webvar{desc} = desanitize($webvar{desc}); | 
|---|
| [4] | 1202 | $html =~ s/\$\$DESC\$\$/$webvar{desc}/g; | 
|---|
| [89] | 1203 | $webvar{notes} = desanitize($webvar{notes}); | 
|---|
| [4] | 1204 | $html =~ s/\$\$NOTES\$\$/$webvar{notes}/g; | 
|---|
|  | 1205 |  | 
|---|
|  | 1206 | print $html; | 
|---|
|  | 1207 |  | 
|---|
|  | 1208 | } # update() | 
|---|
|  | 1209 |  | 
|---|
|  | 1210 |  | 
|---|
|  | 1211 | # Delete an allocation. | 
|---|
| [125] | 1212 | sub remove { | 
|---|
| [4] | 1213 | printHeader(''); | 
|---|
|  | 1214 | #show confirm screen. | 
|---|
|  | 1215 | open HTML, "../confirmRemove.html" | 
|---|
|  | 1216 | or croak "Could not open confirmRemove.html :$!"; | 
|---|
|  | 1217 | my $html = join('', <HTML>); | 
|---|
|  | 1218 | close HTML; | 
|---|
|  | 1219 |  | 
|---|
|  | 1220 | # Serves'em right for getting here... | 
|---|
|  | 1221 | if (!defined($webvar{block})) { | 
|---|
| [125] | 1222 | printError("Error 332"); | 
|---|
|  | 1223 | return; | 
|---|
| [4] | 1224 | } | 
|---|
|  | 1225 |  | 
|---|
| [75] | 1226 | my ($cidr, $custid, $type, $city, $circid, $desc, $notes, $alloctype); | 
|---|
| [4] | 1227 |  | 
|---|
| [192] | 1228 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| [4] | 1229 | $sth = $ip_dbh->prepare("select cidr,city from routed where cidr='$webvar{block}'"); | 
|---|
|  | 1230 | $sth->execute(); | 
|---|
|  | 1231 |  | 
|---|
|  | 1232 | # This feels...  extreme. | 
|---|
|  | 1233 | croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1234 |  | 
|---|
|  | 1235 | $sth->bind_columns(\$cidr,\$city); | 
|---|
|  | 1236 | $sth->execute(); | 
|---|
|  | 1237 | $sth->fetch || croak $sth->errstr(); | 
|---|
|  | 1238 | $custid = "N/A"; | 
|---|
|  | 1239 | $alloctype = $webvar{alloctype}; | 
|---|
| [75] | 1240 | $circid = "N/A"; | 
|---|
| [4] | 1241 | $desc = "N/A"; | 
|---|
|  | 1242 | $notes = "N/A"; | 
|---|
|  | 1243 |  | 
|---|
|  | 1244 | } elsif ($webvar{alloctype} eq 'mm') { | 
|---|
|  | 1245 | $cidr = $webvar{block}; | 
|---|
|  | 1246 | $city = "N/A"; | 
|---|
|  | 1247 | $custid = "N/A"; | 
|---|
|  | 1248 | $alloctype = $webvar{alloctype}; | 
|---|
| [75] | 1249 | $circid = "N/A"; | 
|---|
| [4] | 1250 | $desc = "N/A"; | 
|---|
|  | 1251 | $notes = "N/A"; | 
|---|
| [192] | 1252 | } elsif ($webvar{alloctype} =~ /^.i$/) { # done with alloctype=[rm]m | 
|---|
| [4] | 1253 |  | 
|---|
|  | 1254 | # Unassigning a static IP | 
|---|
| [163] | 1255 | my $sth = $ip_dbh->prepare("select ip,custid,city,type,notes,circuitid from poolips". | 
|---|
| [4] | 1256 | " where ip='$webvar{block}'"); | 
|---|
|  | 1257 | $sth->execute(); | 
|---|
|  | 1258 | #  croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1259 |  | 
|---|
| [75] | 1260 | $sth->bind_columns(\$cidr, \$custid, \$city, \$alloctype, \$notes, \$circid); | 
|---|
| [4] | 1261 | $sth->fetch() || croak $sth->errstr; | 
|---|
|  | 1262 |  | 
|---|
| [159] | 1263 | } else { # done with alloctype=~ /^.i$/ | 
|---|
| [4] | 1264 |  | 
|---|
| [75] | 1265 | my $sth = $ip_dbh->prepare("select cidr,custid,type,city,circuitid,description,notes from ". | 
|---|
| [4] | 1266 | "allocations where cidr='$webvar{block}'"); | 
|---|
|  | 1267 | $sth->execute(); | 
|---|
|  | 1268 | #       croak $sth->errstr() if($sth->errstr()); | 
|---|
|  | 1269 |  | 
|---|
| [75] | 1270 | $sth->bind_columns(\$cidr, \$custid, \$alloctype, \$city, \$circid, \$desc, \$notes); | 
|---|
|  | 1271 | $sth->fetch() || carp $sth->errstr; | 
|---|
| [4] | 1272 | } # end cases for different alloctypes | 
|---|
|  | 1273 |  | 
|---|
|  | 1274 | # Munge everything into HTML | 
|---|
|  | 1275 | $html =~ s|Please confirm|Please confirm <b>removal</b> of|; | 
|---|
|  | 1276 | $html =~ s|\$\$BLOCK\$\$|$cidr|g; | 
|---|
| [98] | 1277 | $html =~ s|\$\$TYPEFULL\$\$|$disp_alloctypes{$alloctype}|g; | 
|---|
| [4] | 1278 | $html =~ s|\$\$ALLOCTYPE\$\$|$alloctype|g; | 
|---|
|  | 1279 | $html =~ s|\$\$CITY\$\$|$city|g; | 
|---|
|  | 1280 | $html =~ s|\$\$CUSTID\$\$|$custid|g; | 
|---|
| [75] | 1281 | $html =~ s|\$\$CIRCID\$\$|$circid|g; | 
|---|
| [4] | 1282 | $html =~ s|\$\$DESC\$\$|$desc|g; | 
|---|
|  | 1283 | $html =~ s|\$\$NOTES\$\$|$notes|g; | 
|---|
|  | 1284 |  | 
|---|
|  | 1285 | $html =~ s|\$\$ACTION\$\$|finaldelete|g; | 
|---|
|  | 1286 |  | 
|---|
|  | 1287 | # Set the warning text. | 
|---|
| [159] | 1288 | if ($alloctype =~ /^.[pd]$/) { | 
|---|
| [4] | 1289 | $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>|; | 
|---|
|  | 1290 | } else { | 
|---|
|  | 1291 | $html =~ s|<!--warn-->|<tr bgcolor="black"><td colspan="2"><div class="red">Warning: clicking confirm will remove this record entirely.</div></td></tr>|; | 
|---|
|  | 1292 | } | 
|---|
|  | 1293 |  | 
|---|
|  | 1294 | print $html; | 
|---|
|  | 1295 | } # end edit() | 
|---|
|  | 1296 |  | 
|---|
|  | 1297 |  | 
|---|
|  | 1298 | # Delete an allocation.  Return it to the freeblocks table;  munge | 
|---|
|  | 1299 | # data as necessary to keep as few records as possible in freeblocks | 
|---|
|  | 1300 | # to prevent weirdness when allocating blocks later. | 
|---|
|  | 1301 | # Remove IPs from pool listing if necessary | 
|---|
|  | 1302 | sub finalDelete { | 
|---|
|  | 1303 | printHeader(''); | 
|---|
|  | 1304 |  | 
|---|
| [125] | 1305 | my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype}); | 
|---|
| [4] | 1306 |  | 
|---|
| [125] | 1307 | if ($code eq 'OK') { | 
|---|
| [4] | 1308 | print "<div class=heading align=center>Success!  $webvar{block} deallocated.</div>\n"; | 
|---|
| [125] | 1309 | syslog "notice", "$authuser deallocated '$webvar{alloctype}'-type netblock $webvar{block}"; | 
|---|
|  | 1310 | } else { | 
|---|
|  | 1311 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 1312 | syslog "err", "$authuser could not deallocate static IP '$webvar{block}': '$msg'"; | 
|---|
|  | 1313 | printError("Could not deallocate static IP $webvar{block}: $msg"); | 
|---|
|  | 1314 | } else { | 
|---|
|  | 1315 | syslog "err", "$authuser could not deallocate netblock '$webvar{block}': '$msg'"; | 
|---|
|  | 1316 | printError("Could not deallocate netblock $webvar{block}: $msg"); | 
|---|
| [4] | 1317 | } | 
|---|
| [102] | 1318 | } | 
|---|
|  | 1319 |  | 
|---|
| [4] | 1320 | } # finalDelete | 
|---|
|  | 1321 |  | 
|---|
|  | 1322 |  | 
|---|
|  | 1323 | # Just in case we manage to get here. | 
|---|
|  | 1324 | exit 0; | 
|---|