| 1 | #!/usr/bin/perl | 
|---|
| 2 | # ipdb/cgi-bin/admin.cgi | 
|---|
| 3 | # Hack interface to make specific changes to IPDB that (for one reason | 
|---|
| 4 | # or another) can't be made through the main interface. | 
|---|
| 5 | # | 
|---|
| 6 | ### | 
|---|
| 7 | # SVN revision info | 
|---|
| 8 | # $Date: 2005-05-30 15:30:46 +0000 (Mon, 30 May 2005) $ | 
|---|
| 9 | # SVN revision $Rev: 252 $ | 
|---|
| 10 | # Last update by $Author: kdeugau $ | 
|---|
| 11 | ### | 
|---|
| 12 | # Copyright (C) 2004,2005 - Kris Deugau | 
|---|
| 13 |  | 
|---|
| 14 | use strict; | 
|---|
| 15 | use warnings; | 
|---|
| 16 | use CGI::Carp qw(fatalsToBrowser); | 
|---|
| 17 | use DBI; | 
|---|
| 18 | use CommonWeb qw(:ALL); | 
|---|
| 19 | use MyIPDB; | 
|---|
| 20 | use CustIDCK; | 
|---|
| 21 | #use POSIX qw(ceil); | 
|---|
| 22 | use NetAddr::IP; | 
|---|
| 23 |  | 
|---|
| 24 | use Sys::Syslog; | 
|---|
| 25 |  | 
|---|
| 26 | openlog "IPDB-admin","pid","local2"; | 
|---|
| 27 |  | 
|---|
| 28 | # Collect the username from HTTP auth.  If undefined, we're in a test environment. | 
|---|
| 29 | my $authuser; | 
|---|
| 30 | if (!defined($ENV{'REMOTE_USER'})) { | 
|---|
| 31 | $authuser = '__temptest'; | 
|---|
| 32 | } else { | 
|---|
| 33 | $authuser = $ENV{'REMOTE_USER'}; | 
|---|
| 34 | } | 
|---|
| 35 |  | 
|---|
| 36 | syslog "debug", "$authuser active"; | 
|---|
| 37 |  | 
|---|
| 38 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
| 39 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
| 40 | my $ip_dbh; | 
|---|
| 41 | my $sth; | 
|---|
| 42 | my $errstr; | 
|---|
| 43 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
| 44 | if (!$ip_dbh) { | 
|---|
| 45 | printAndExit("Database error: $errstr\n"); | 
|---|
| 46 | } | 
|---|
| 47 | initIPDBGlobals($ip_dbh); | 
|---|
| 48 |  | 
|---|
| 49 | if ($IPDBacl{$authuser} !~ /A/) { | 
|---|
| 50 | print "Content-Type: text/html\n\n". | 
|---|
| 51 | "<html><head><title>Access denied</title></head><body>\n". | 
|---|
| 52 | 'Access to this tool is restricted.  Contact <a href="mailto:kdeugau@vianet.ca">Kris</a> '. | 
|---|
| 53 | "for more information.</body></html>\n"; | 
|---|
| 54 | exit; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | my %webvar = parse_post(); | 
|---|
| 58 | cleanInput(\%webvar); | 
|---|
| 59 |  | 
|---|
| 60 | print "Content-type: text/html\n\n". | 
|---|
| 61 | "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n". | 
|---|
| 62 | qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n). | 
|---|
| 63 | "</head>\n<body>\n". | 
|---|
| 64 | "<h2>IPDB - Administrative Tools</h2>\n<hr>\n"; | 
|---|
| 65 |  | 
|---|
| 66 | if(!defined($webvar{action})) { | 
|---|
| 67 | $webvar{action} = "<NULL>";   #shuts up the warnings. | 
|---|
| 68 |  | 
|---|
| 69 | my $typelist = ''; | 
|---|
| 70 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder"); | 
|---|
| 71 | $sth->execute; | 
|---|
| 72 | my @data = $sth->fetchrow_array; | 
|---|
| 73 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n"; | 
|---|
| 74 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 75 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n"; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the | 
|---|
| 79 | main interface if at all possible. | 
|---|
| 80 | <hr> | 
|---|
| 81 | <a href="admin.cgi?action=newalloc">Add allocation</a> | 
|---|
| 82 | <hr> | 
|---|
| 83 | <form action="admin.cgi" method="POST"> | 
|---|
| 84 | <input type=hidden name=action value=alloc> | 
|---|
| 85 | Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid> | 
|---|
| 86 | <input type=submit value=" GIMME!! "></form> | 
|---|
| 87 | <hr><form action="admin.cgi" method="POST"> | 
|---|
| 88 | <input type=hidden name=action value=alloctweak> | 
|---|
| 89 | Manually update allocation data in this /24: <input name=allocfrom> | 
|---|
| 90 | <input type=submit value="Show allocations"> | 
|---|
| 91 | </form> | 
|---|
| 92 | <hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates | 
|---|
| 93 | <hr><a href="admin.cgi?action=showACL">Change ACLs</a> (change internal access controls - | 
|---|
| 94 | note that this does NOT include IP-based limits) | 
|---|
| 95 | ); | 
|---|
| 96 | } else { | 
|---|
| 97 | print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>'; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | ## Possible actions. | 
|---|
| 102 | if ($webvar{action} eq 'alloc') { | 
|---|
| 103 | # OK, we know what we're allocating. | 
|---|
| 104 |  | 
|---|
| 105 | if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) { | 
|---|
| 106 | printAndExit("Can't allocate something that's not a netblock/ip"); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'"); | 
|---|
| 110 | $sth->execute; | 
|---|
| 111 | my @data = $sth->fetchrow_array; | 
|---|
| 112 | my $custid = $data[0]; | 
|---|
| 113 | if ($custid eq '') { | 
|---|
| 114 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) { | 
|---|
| 115 | # Force uppercase for now... | 
|---|
| 116 | $webvar{custid} =~ tr/a-z/A-Z/; | 
|---|
| 117 | # Crosscheck with ... er...  something. | 
|---|
| 118 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
| 119 | if ($CustIDCK::Error) { | 
|---|
| 120 | printError("Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
| 121 | return; | 
|---|
| 122 | } | 
|---|
| 123 | if (!$status) { | 
|---|
| 124 | printError("Customer ID not valid.  Make sure the Customer ID ". | 
|---|
| 125 | "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ". | 
|---|
| 126 | "non-customer assignments."); | 
|---|
| 127 | return; | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 | # Type that doesn't have a default custid | 
|---|
| 131 | $custid = $webvar{custid}; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
| 135 | my @data; | 
|---|
| 136 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| 137 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'"); | 
|---|
| 138 | $sth->execute; | 
|---|
| 139 | @data = $sth->fetchrow_array; | 
|---|
| 140 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
| 141 | printAndExit("Can't allocate from outside a free block!!\n") | 
|---|
| 142 | if !$data[0]; | 
|---|
| 143 | } elsif ($webvar{alloctype} =~ /^(.)i$/) { | 
|---|
| 144 | $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')"); | 
|---|
| 145 | $sth->execute; | 
|---|
| 146 | @data = $sth->fetchrow_array; | 
|---|
| 147 | # User deserves errors if user can't be bothered to find the pool and a free IP first. | 
|---|
| 148 | printAndExit("Can't allocate static IP from outside a pool!!\n") | 
|---|
| 149 | if !$data[0]; | 
|---|
| 150 | } else { | 
|---|
| 151 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')"); | 
|---|
| 152 | $sth->execute; | 
|---|
| 153 | @data = $sth->fetchrow_array; | 
|---|
| 154 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
| 155 | printAndExit("Can't allocate from outside a routed block!!\n") | 
|---|
| 156 | if !$data[0]; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | my $alloc_from = new NetAddr::IP $data[0]; | 
|---|
| 160 | $sth->finish; | 
|---|
| 161 |  | 
|---|
| 162 | my $cities = ''; | 
|---|
| 163 | foreach my $city (@citylist) { | 
|---|
| 164 | $cities .= "<option>$city</option>\n"; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | print qq(<table class=regular> | 
|---|
| 168 | <form method=POST action=admin.cgi> | 
|---|
| 169 | <tr class=color1> | 
|---|
| 170 | <td>Allocating:</td> | 
|---|
| 171 | <td>$cidr<input type=hidden name=cidr value="$cidr"></td> | 
|---|
| 172 | </tr><tr class=color2> | 
|---|
| 173 | <td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}} | 
|---|
| 174 | <input type=hidden name=alloctype value="$webvar{alloctype}"></td> | 
|---|
| 175 | </tr><tr class=color1> | 
|---|
| 176 | <td>Allocated from:</td> | 
|---|
| 177 | <td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td> | 
|---|
| 178 | </tr><tr class="color2"> | 
|---|
| 179 | <td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td> | 
|---|
| 180 | </tr><tr class=color1> | 
|---|
| 181 | <td>Customer location:</td><td> | 
|---|
| 182 | <select name="city"><option selected>-</option> | 
|---|
| 183 | $cities | 
|---|
| 184 | </select> | 
|---|
| 185 |  <a href="javascript:popNotes('/ip/newcity.html')">Add new location</a> | 
|---|
| 186 | </td> | 
|---|
| 187 | </tr> | 
|---|
| 188 | <tr class="color2"> | 
|---|
| 189 | <td>Circuit ID:</td><td><input name=circid size=40></td> | 
|---|
| 190 | </tr><tr class="color1"> | 
|---|
| 191 | <td>Description/Name:</td><td><input name="desc" size=40></td> | 
|---|
| 192 | </tr><tr class="color2"> | 
|---|
| 193 | <td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td> | 
|---|
| 194 | </tr><tr class="warning"> | 
|---|
| 195 | <td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td> | 
|---|
| 196 | </tr><tr class="color2"> | 
|---|
| 197 | <td class="center" colspan="2"><input type="submit" value="  Assign  "></td> | 
|---|
| 198 | <input type="hidden" name="action" value="confirm"> | 
|---|
| 199 | </tr> | 
|---|
| 200 | </table> | 
|---|
| 201 | ); | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | } elsif ($webvar{action} eq 'confirm') { | 
|---|
| 205 |  | 
|---|
| 206 | print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ". | 
|---|
| 207 | "$disp_alloctypes{$webvar{alloctype}}...<br>\n"; | 
|---|
| 208 | # Only need to check city here. | 
|---|
| 209 | if ($webvar{city} eq '-') { | 
|---|
| 210 | printError("Invalid customer location!  Go back and select customer's location."); | 
|---|
| 211 | } else { | 
|---|
| 212 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from}, | 
|---|
| 213 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
| 214 | $webvar{circid}); | 
|---|
| 215 | if ($retcode eq 'OK') { | 
|---|
| 216 | print "Allocation OK!\n"; | 
|---|
| 217 |  | 
|---|
| 218 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| 219 | # Notify tech@example.com | 
|---|
| 220 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
| 221 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
| 222 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| 223 | } | 
|---|
| 224 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 225 | "'$webvar{alloctype}'"; | 
|---|
| 226 | } else { | 
|---|
| 227 | print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n"; | 
|---|
| 228 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 229 | "'$webvar{alloctype}' failed: '$msg'"; | 
|---|
| 230 | } | 
|---|
| 231 | } # done city check | 
|---|
| 232 |  | 
|---|
| 233 | } elsif ($webvar{action} eq 'alloctweak') { | 
|---|
| 234 | fix_allocfrom(); | 
|---|
| 235 | showAllocs($webvar{allocfrom}); | 
|---|
| 236 | } elsif ($webvar{action} eq 'update') { | 
|---|
| 237 | update(); | 
|---|
| 238 | } elsif ($webvar{action} eq 'assign') { | 
|---|
| 239 | # Display a list of possible blocks within the requested block. | 
|---|
| 240 | open (HTML, "../admin_alloc.html") | 
|---|
| 241 | or croak "Could not open admin_alloc.html :$!"; | 
|---|
| 242 | my $html = join('', <HTML>); | 
|---|
| 243 | $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g; | 
|---|
| 244 | $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g; | 
|---|
| 245 |  | 
|---|
| 246 | my $from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
| 247 | my @blocklist = $from->split($webvar{masklen}); | 
|---|
| 248 | my $availblocks; | 
|---|
| 249 | foreach (@blocklist) { | 
|---|
| 250 | $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n); | 
|---|
| 251 | } | 
|---|
| 252 | $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g; | 
|---|
| 253 |  | 
|---|
| 254 | print $html; | 
|---|
| 255 | } elsif ($webvar{action} eq 'showpools') { | 
|---|
| 256 | print "IP Pools currently allocated:\n". | 
|---|
| 257 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n"; | 
|---|
| 258 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr"); | 
|---|
| 259 | $sth->execute; | 
|---|
| 260 | my %poolfree; | 
|---|
| 261 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 262 | $poolfree{$data[0]} = 0; | 
|---|
| 263 | } | 
|---|
| 264 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip"); | 
|---|
| 265 | $sth->execute; | 
|---|
| 266 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 267 | $poolfree{$data[0]}++; | 
|---|
| 268 | } | 
|---|
| 269 | foreach my $key (keys %poolfree) { | 
|---|
| 270 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>). | 
|---|
| 271 | "<td>$poolfree{$key}</td></tr>\n"; | 
|---|
| 272 | } | 
|---|
| 273 | print "</table>\n"; | 
|---|
| 274 | } elsif ($webvar{action} eq 'tweakpool') { | 
|---|
| 275 | showPool($webvar{pool}); | 
|---|
| 276 | } elsif ($webvar{action} eq 'updatepool') { | 
|---|
| 277 |  | 
|---|
| 278 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ". | 
|---|
| 279 | "city='$webvar{city}', type='$webvar{type}', available='". | 
|---|
| 280 | (($webvar{available} eq 'y') ? 'y' : 'n'). | 
|---|
| 281 | "', notes='$webvar{notes}', description='$webvar{desc}' ". | 
|---|
| 282 | "where ip='$webvar{ip}'"); | 
|---|
| 283 | $sth->execute; | 
|---|
| 284 | if ($sth->err) { | 
|---|
| 285 | print "Error updating pool IP $webvar{ip}: $@<hr>\n"; | 
|---|
| 286 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@"; | 
|---|
| 287 | } else { | 
|---|
| 288 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'"); | 
|---|
| 289 | $sth->execute; | 
|---|
| 290 | my @data = $sth->fetchrow_array; | 
|---|
| 291 | print "$webvar{ip} in $data[0] updated\n<hr>\n"; | 
|---|
| 292 | syslog "notice", "$authuser updated pool IP $webvar{ip}"; | 
|---|
| 293 | } | 
|---|
| 294 | } elsif ($webvar{action} eq 'showACL') { | 
|---|
| 295 | print "Notes:<br>\n". | 
|---|
| 296 | "<li>Users must be added to .htpasswd from the shell, for the time being.\n". | 
|---|
| 297 | "<li>New accounts will be added to the ACL here every time this page is loaded.\n". | 
|---|
| 298 | "<li>Old accounts will NOT be automatically deleted;  they must be removed via shell.\n". | 
|---|
| 299 | "<li>Admin users automatically get all other priviledges.\n"; | 
|---|
| 300 | # open .htpasswd, and snag the userlist. | 
|---|
| 301 | $sth = $ip_dbh->prepare("select count (*) from users where username=?"); | 
|---|
| 302 | open HTPASS, "<../../.htpasswd" or carp "BOO! No .htpasswd file!"; | 
|---|
| 303 | while (<HTPASS>) { | 
|---|
| 304 | chomp; | 
|---|
| 305 | my ($username,$encpwd) = split /:/; | 
|---|
| 306 | $sth->execute($username); | 
|---|
| 307 | my @data = $sth->fetchrow_array; | 
|---|
| 308 | if ($data[0] eq '0') { | 
|---|
| 309 | my $sth2 = $ip_dbh->prepare("insert into users (username,password) values ('$username','$encpwd')"); | 
|---|
| 310 | $sth2->execute; | 
|---|
| 311 | print "$username added with read-only privs to ACL<br>\n"; | 
|---|
| 312 | } | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | print "<hr>Users with access:\n<table border=1>\n"; | 
|---|
| 316 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>". | 
|---|
| 317 | "<td>Delete</td><td>Admin user</td></tr>\n". | 
|---|
| 318 | "<form action=admin.cgi method=POST>\n"; | 
|---|
| 319 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); | 
|---|
| 320 | $sth->execute; | 
|---|
| 321 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 322 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>". | 
|---|
| 323 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>). | 
|---|
| 324 | # Now for the fun bit.  We have to pull apart the ACL field and | 
|---|
| 325 | # output a bunch of checkboxes. | 
|---|
| 326 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : ''). | 
|---|
| 327 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : ''). | 
|---|
| 328 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : ''). | 
|---|
| 329 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : ''). | 
|---|
| 330 | qq(></td><td><input type=submit value="Update"></td></tr></form>\n); | 
|---|
| 331 |  | 
|---|
| 332 | } | 
|---|
| 333 | print "</table>\n"; | 
|---|
| 334 | } elsif ($webvar{action} eq 'updacl') { | 
|---|
| 335 | print "Updating ACL for $webvar{username}:<br>\n"; | 
|---|
| 336 | my $acl = 'b'; | 
|---|
| 337 | if ($webvar{admin} eq 'on') { | 
|---|
| 338 | $acl .= "acdA"; | 
|---|
| 339 | } else { | 
|---|
| 340 | $acl .= ($webvar{add} eq 'on' ? 'a' : ''). | 
|---|
| 341 | ($webvar{change} eq 'on' ? 'c' : ''). | 
|---|
| 342 | ($webvar{del} eq 'on' ? 'd' : ''); | 
|---|
| 343 | } | 
|---|
| 344 | print "New ACL: $acl<br>\n"; | 
|---|
| 345 |  | 
|---|
| 346 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'"); | 
|---|
| 347 | $sth->execute; | 
|---|
| 348 | print "OK\n" if !$sth->err; | 
|---|
| 349 |  | 
|---|
| 350 | print qq(<hr><a href="admin.cgi?action=showACL">Back</a> to ACL listing\n); | 
|---|
| 351 |  | 
|---|
| 352 | } elsif ($webvar{action} ne '<NULL>') { | 
|---|
| 353 | print "webvar{action} check failed: Don't know how to $webvar{action}"; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | # Hokay.  This is a little different.  We have a few specific functions here: | 
|---|
| 357 | #  -> Assign arbitrary subnet from arbitrary free space | 
|---|
| 358 | #  -> Tweak individual DB fields | 
|---|
| 359 | # | 
|---|
| 360 |  | 
|---|
| 361 |  | 
|---|
| 362 | printFooter; | 
|---|
| 363 |  | 
|---|
| 364 | $ip_dbh->disconnect; | 
|---|
| 365 |  | 
|---|
| 366 | exit; | 
|---|
| 367 |  | 
|---|
| 368 |  | 
|---|
| 369 | # Tweak allocfrom into shape. | 
|---|
| 370 | sub fix_allocfrom { | 
|---|
| 371 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) { | 
|---|
| 372 | # 3-octet class C specified | 
|---|
| 373 | $webvar{allocfrom} .= ".0/24"; | 
|---|
| 374 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) { | 
|---|
| 375 | # 4-octet IP specified; | 
|---|
| 376 | $webvar{allocfrom} .= "/24"; | 
|---|
| 377 | } | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 |  | 
|---|
| 381 | # List free blocks in a /24 for arbitrary manual allocation | 
|---|
| 382 | sub showfree($) { | 
|---|
| 383 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
| 384 | print "Showing free blocks in $cidr<br>\n". | 
|---|
| 385 | "<table border=1>\n"; | 
|---|
| 386 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr"); | 
|---|
| 387 | $sth->execute; | 
|---|
| 388 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 389 | my $temp = new NetAddr::IP $data[0]; | 
|---|
| 390 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n". | 
|---|
| 391 | qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n). | 
|---|
| 392 | "<td>". | 
|---|
| 393 | (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30' | 
|---|
| 394 | : "<select name=masklen><option>30</option>\n<option>29</option>\n") . | 
|---|
| 395 | (($temp->masklen < 29) ? "<option>28</option>\n" : '') . | 
|---|
| 396 | (($temp->masklen < 28) ? "<option>27</option>\n" : '') . | 
|---|
| 397 | (($temp->masklen < 27) ? "<option>26</option>\n" : '') . | 
|---|
| 398 | (($temp->masklen < 26) ? "<option>25</option>\n" : '') . | 
|---|
| 399 | (($temp->masklen < 25) ? "<option>24</option>\n" : '') . | 
|---|
| 400 | "</td>". | 
|---|
| 401 | qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>). | 
|---|
| 402 | "\n</form></tr>\n"; | 
|---|
| 403 | } | 
|---|
| 404 | print "</table>\n"; | 
|---|
| 405 | } | 
|---|
| 406 |  | 
|---|
| 407 |  | 
|---|
| 408 | # Show allocations to allow editing. | 
|---|
| 409 | sub showAllocs($) { | 
|---|
| 410 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
| 411 | print "Edit custID, allocation type, city for allocations in ". | 
|---|
| 412 | "$cidr:\n<table border=1>"; | 
|---|
| 413 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr"); | 
|---|
| 414 | $sth->execute; | 
|---|
| 415 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 416 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n". | 
|---|
| 417 | qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n). | 
|---|
| 418 | qq(<td><input name=custid value="$data[1]"></td>\n); | 
|---|
| 419 |  | 
|---|
| 420 | print "<td><select name=alloctype><option". | 
|---|
| 421 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option". | 
|---|
| 422 | (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option". | 
|---|
| 423 | (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option". | 
|---|
| 424 | (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option". | 
|---|
| 425 | (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option". | 
|---|
| 426 | (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option". | 
|---|
| 427 | (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option". | 
|---|
| 428 | (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option". | 
|---|
| 429 | (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option". | 
|---|
| 430 | (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option". | 
|---|
| 431 | (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option". | 
|---|
| 432 | (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option". | 
|---|
| 433 | (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". | 
|---|
| 434 | (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option". | 
|---|
| 435 | (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option". | 
|---|
| 436 | (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option". | 
|---|
| 437 | (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". | 
|---|
| 438 | "</select></td>\n"; | 
|---|
| 439 | print qq(<td><input name=city value="$data[3]"></td>\n). | 
|---|
| 440 | "<td>$data[4]</td><td>$data[5]</td>". | 
|---|
| 441 | qq(<td><input type=submit value="Update"></td></form></tr>\n); | 
|---|
| 442 | } | 
|---|
| 443 | print "</table>\n"; | 
|---|
| 444 |  | 
|---|
| 445 | # notes | 
|---|
| 446 | print "<hr><b>Notes:</b>\n". | 
|---|
| 447 | "<ul>\n<li>Use the main interface to update description and notes fields\n". | 
|---|
| 448 | "<li>Changing the allocation type here will NOT affect IP pool data.\n". | 
|---|
| 449 | "</ul>\n"; | 
|---|
| 450 | } | 
|---|
| 451 |  | 
|---|
| 452 |  | 
|---|
| 453 | # Stuff updates into DB | 
|---|
| 454 | sub update { | 
|---|
| 455 | eval { | 
|---|
| 456 | # Relatively simple SQL transaction here.  Note that we're deliberately NOT | 
|---|
| 457 | # updating notes/desc here as it's available through the main interface. | 
|---|
| 458 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',". | 
|---|
| 459 | "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'"); | 
|---|
| 460 | $sth->execute; | 
|---|
| 461 | $ip_dbh->commit; | 
|---|
| 462 | }; | 
|---|
| 463 | if ($@) { | 
|---|
| 464 | carp "Transaction aborted because $@"; | 
|---|
| 465 | eval { $ip_dbh->rollback; }; | 
|---|
| 466 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'"; | 
|---|
| 467 | } else { | 
|---|
| 468 | # If we get here, the operation succeeded. | 
|---|
| 469 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
| 470 | print "Allocation $webvar{block} updated<hr>\n"; | 
|---|
| 471 | } | 
|---|
| 472 | # need to get /24 that block is part of | 
|---|
| 473 | my @bits = split /\./, $webvar{block}; | 
|---|
| 474 | $bits[3] = "0/24"; | 
|---|
| 475 | showAllocs((join ".", @bits)); | 
|---|
| 476 | } | 
|---|
| 477 |  | 
|---|
| 478 |  | 
|---|
| 479 | # showPool() | 
|---|
| 480 | # List all IPs in a pool, and allow arbitrary admin changes to each | 
|---|
| 481 | # Allow changes to ALL fields | 
|---|
| 482 | sub showPool($) { | 
|---|
| 483 | my $pool = new NetAddr::IP $_[0]; | 
|---|
| 484 | print qq(Listing pool $pool:\n<table border=1> | 
|---|
| 485 | <form action=admin.cgi method=POST> | 
|---|
| 486 | <input type=hidden name=action value=updatepool> | 
|---|
| 487 | <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr> | 
|---|
| 488 | <tr><td align=right>Customer location:</td><td><input name=city></td></tr> | 
|---|
| 489 | <tr><td align=right>Type:</td><td><select name=type><option selected>-</option> | 
|---|
| 490 | <option value="si">Static IP - Server pool</option> | 
|---|
| 491 | <option value="ci">Static IP - Cable</option> | 
|---|
| 492 | <option value="di">Static IP - DSL</option> | 
|---|
| 493 | <option value="mi">Static IP - Dialup</option> | 
|---|
| 494 | <option value="wi">Static IP - Wireless</option> | 
|---|
| 495 | </select></td></tr> | 
|---|
| 496 | <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr> | 
|---|
| 497 | <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr> | 
|---|
| 498 | <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr> | 
|---|
| 499 | <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr> | 
|---|
| 500 | ). | 
|---|
| 501 | "</table>Update the following record:<table border=1>\n"; | 
|---|
| 502 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip"); | 
|---|
| 503 | $sth->execute; | 
|---|
| 504 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 505 | print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>). | 
|---|
| 506 | "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>". | 
|---|
| 507 | "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n"; | 
|---|
| 508 | } | 
|---|
| 509 | print "</form></table>\n"; | 
|---|
| 510 | } | 
|---|