[54] | 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.
|
---|
[65] | 5 | #
|
---|
[54] | 6 | ###
|
---|
| 7 | # SVN revision info
|
---|
| 8 | # $Date: 2005-09-21 20:30:31 +0000 (Wed, 21 Sep 2005) $
|
---|
| 9 | # SVN revision $Rev: 284 $
|
---|
| 10 | # Last update by $Author: kdeugau $
|
---|
| 11 | ###
|
---|
[146] | 12 | # Copyright (C) 2004,2005 - Kris Deugau
|
---|
[54] | 13 |
|
---|
| 14 | use strict;
|
---|
| 15 | use warnings;
|
---|
| 16 | use CGI::Carp qw(fatalsToBrowser);
|
---|
| 17 | use DBI;
|
---|
| 18 | use CommonWeb qw(:ALL);
|
---|
[199] | 19 | use MyIPDB;
|
---|
[54] | 20 | #use POSIX qw(ceil);
|
---|
| 21 | use NetAddr::IP;
|
---|
| 22 |
|
---|
| 23 | use Sys::Syslog;
|
---|
| 24 |
|
---|
| 25 | openlog "IPDB-admin","pid","local2";
|
---|
| 26 |
|
---|
| 27 | # Collect the username from HTTP auth. If undefined, we're in a test environment.
|
---|
| 28 | my $authuser;
|
---|
| 29 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
| 30 | $authuser = '__temptest';
|
---|
| 31 | } else {
|
---|
| 32 | $authuser = $ENV{'REMOTE_USER'};
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | syslog "debug", "$authuser active";
|
---|
| 36 |
|
---|
[199] | 37 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
| 38 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
| 39 | my $ip_dbh;
|
---|
| 40 | my $sth;
|
---|
| 41 | my $errstr;
|
---|
| 42 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
| 43 | if (!$ip_dbh) {
|
---|
| 44 | printAndExit("Database error: $errstr\n");
|
---|
| 45 | }
|
---|
| 46 | initIPDBGlobals($ip_dbh);
|
---|
| 47 |
|
---|
[233] | 48 | if ($IPDBacl{$authuser} !~ /A/) {
|
---|
| 49 | print "Content-Type: text/html\n\n".
|
---|
| 50 | "<html><head><title>Access denied</title></head><body>\n".
|
---|
| 51 | 'Access to this tool is restricted. Contact <a href="mailto:kdeugau@vianet.ca">Kris</a> '.
|
---|
| 52 | "for more information.</body></html>\n";
|
---|
| 53 | exit;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[54] | 56 | my %webvar = parse_post();
|
---|
| 57 | cleanInput(\%webvar);
|
---|
[58] | 58 |
|
---|
[54] | 59 | print "Content-type: text/html\n\n".
|
---|
[199] | 60 | "<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n".
|
---|
| 61 | qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
|
---|
| 62 | "</head>\n<body>\n".
|
---|
[54] | 63 | "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
|
---|
| 64 |
|
---|
| 65 | if(!defined($webvar{action})) {
|
---|
| 66 | $webvar{action} = "<NULL>"; #shuts up the warnings.
|
---|
[199] | 67 |
|
---|
| 68 | my $typelist = '';
|
---|
| 69 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
|
---|
| 70 | $sth->execute;
|
---|
| 71 | my @data = $sth->fetchrow_array;
|
---|
| 72 | $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
|
---|
| 73 | while (my @data = $sth->fetchrow_array) {
|
---|
| 74 | $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[54] | 77 | print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
|
---|
| 78 | main interface if at all possible.
|
---|
[199] | 79 | <hr>
|
---|
| 80 | <a href="admin.cgi?action=newalloc">Add allocation</a>
|
---|
| 81 | <hr>
|
---|
| 82 | <form action="admin.cgi" method="POST">
|
---|
[54] | 83 | <input type=hidden name=action value=alloc>
|
---|
[199] | 84 | Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
|
---|
| 85 | <input type=submit value=" GIMME!! "></form>
|
---|
[54] | 86 | <hr><form action="admin.cgi" method="POST">
|
---|
| 87 | <input type=hidden name=action value=alloctweak>
|
---|
| 88 | Manually update allocation data in this /24: <input name=allocfrom>
|
---|
| 89 | <input type=submit value="Show allocations">
|
---|
[65] | 90 | </form>
|
---|
| 91 | <hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
|
---|
[258] | 92 | <hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users; change
|
---|
| 93 | internal access controls - note that this does NOT include IP-based limits)
|
---|
[54] | 94 | );
|
---|
| 95 | } else {
|
---|
| 96 | print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[199] | 99 |
|
---|
| 100 | ## Possible actions.
|
---|
[54] | 101 | if ($webvar{action} eq 'alloc') {
|
---|
[199] | 102 | # OK, we know what we're allocating.
|
---|
| 103 |
|
---|
| 104 | if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
|
---|
| 105 | printAndExit("Can't allocate something that's not a netblock/ip");
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
|
---|
| 109 | $sth->execute;
|
---|
| 110 | my @data = $sth->fetchrow_array;
|
---|
| 111 | my $custid = $data[0];
|
---|
| 112 | if ($custid eq '') {
|
---|
| 113 | # Type that doesn't have a default custid
|
---|
| 114 | $custid = $webvar{custid};
|
---|
| 115 | }
|
---|
| 116 | ##fixme Check billing DB here
|
---|
| 117 |
|
---|
| 118 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
| 119 | my @data;
|
---|
| 120 | if ($webvar{alloctype} eq 'rm') {
|
---|
| 121 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
|
---|
| 122 | $sth->execute;
|
---|
| 123 | @data = $sth->fetchrow_array;
|
---|
| 124 | # User deserves errors if user can't be bothered to find the free block first.
|
---|
| 125 | printAndExit("Can't allocate from outside a free block!!\n")
|
---|
| 126 | if !$data[0];
|
---|
[256] | 127 | } elsif ($webvar{alloctype} =~ /^(.)i$/) {
|
---|
| 128 | $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
|
---|
| 129 | $sth->execute;
|
---|
| 130 | @data = $sth->fetchrow_array;
|
---|
| 131 | # User deserves errors if user can't be bothered to find the pool and a free IP first.
|
---|
| 132 | printAndExit("Can't allocate static IP from outside a pool!!\n")
|
---|
| 133 | if !$data[0];
|
---|
[199] | 134 | } else {
|
---|
| 135 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
|
---|
| 136 | $sth->execute;
|
---|
| 137 | @data = $sth->fetchrow_array;
|
---|
| 138 | # User deserves errors if user can't be bothered to find the free block first.
|
---|
| 139 | printAndExit("Can't allocate from outside a routed block!!\n")
|
---|
| 140 | if !$data[0];
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | my $alloc_from = new NetAddr::IP $data[0];
|
---|
| 144 | $sth->finish;
|
---|
| 145 |
|
---|
| 146 | my $cities = '';
|
---|
| 147 | foreach my $city (@citylist) {
|
---|
| 148 | $cities .= "<option>$city</option>\n";
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | print qq(<table class=regular>
|
---|
| 152 | <form method=POST action=admin.cgi>
|
---|
| 153 | <tr class=color1>
|
---|
| 154 | <td>Allocating:</td>
|
---|
| 155 | <td>$cidr<input type=hidden name=cidr value="$cidr"></td>
|
---|
| 156 | </tr><tr class=color2>
|
---|
| 157 | <td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
|
---|
| 158 | <input type=hidden name=alloctype value="$webvar{alloctype}"></td>
|
---|
| 159 | </tr><tr class=color1>
|
---|
| 160 | <td>Allocated from:</td>
|
---|
| 161 | <td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
|
---|
| 162 | </tr><tr class="color2">
|
---|
| 163 | <td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
|
---|
| 164 | </tr><tr class=color1>
|
---|
| 165 | <td>Customer location:</td><td>
|
---|
| 166 | <select name="city"><option selected>-</option>
|
---|
| 167 | $cities
|
---|
| 168 | </select>
|
---|
| 169 | <a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
|
---|
| 170 | </td>
|
---|
| 171 | </tr>
|
---|
| 172 | <tr class="color2">
|
---|
| 173 | <td>Circuit ID:</td><td><input name=circid size=40></td>
|
---|
| 174 | </tr><tr class="color1">
|
---|
| 175 | <td>Description/Name:</td><td><input name="desc" size=40></td>
|
---|
| 176 | </tr><tr class="color2">
|
---|
| 177 | <td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
|
---|
| 178 | </tr><tr class="warning">
|
---|
| 179 | <td colspan=2><center>WARNING: This will IMMEDIATELY assign this block!!</center></td>
|
---|
| 180 | </tr><tr class="color2">
|
---|
| 181 | <td class="center" colspan="2"><input type="submit" value=" Assign "></td>
|
---|
| 182 | <input type="hidden" name="action" value="confirm">
|
---|
| 183 | </tr>
|
---|
| 184 | </table>
|
---|
| 185 | );
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | } elsif ($webvar{action} eq 'confirm') {
|
---|
| 189 |
|
---|
| 190 | print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
|
---|
| 191 | "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
|
---|
| 192 | # Only need to check city here.
|
---|
| 193 | if ($webvar{city} eq '-') {
|
---|
| 194 | printError("Invalid customer location! Go back and select customer's location.");
|
---|
| 195 | } else {
|
---|
| 196 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
|
---|
| 197 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
| 198 | $webvar{circid});
|
---|
| 199 | if ($retcode eq 'OK') {
|
---|
| 200 | print "Allocation OK!\n";
|
---|
| 201 |
|
---|
| 202 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
| 203 | # Notify tech@example.com
|
---|
| 204 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
| 205 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
|
---|
| 206 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
| 207 | }
|
---|
| 208 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
|
---|
| 209 | "'$webvar{alloctype}'";
|
---|
| 210 | } else {
|
---|
| 211 | print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
|
---|
| 212 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
|
---|
[256] | 213 | "'$webvar{alloctype}' failed: '$msg'";
|
---|
[199] | 214 | }
|
---|
| 215 | } # done city check
|
---|
| 216 |
|
---|
[54] | 217 | } elsif ($webvar{action} eq 'alloctweak') {
|
---|
| 218 | fix_allocfrom();
|
---|
| 219 | showAllocs($webvar{allocfrom});
|
---|
| 220 | } elsif ($webvar{action} eq 'update') {
|
---|
| 221 | update();
|
---|
[58] | 222 | } elsif ($webvar{action} eq 'assign') {
|
---|
| 223 | # Display a list of possible blocks within the requested block.
|
---|
| 224 | open (HTML, "../admin_alloc.html")
|
---|
| 225 | or croak "Could not open admin_alloc.html :$!";
|
---|
| 226 | my $html = join('', <HTML>);
|
---|
| 227 | $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
|
---|
| 228 | $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
|
---|
| 229 |
|
---|
| 230 | my $from = new NetAddr::IP $webvar{allocfrom};
|
---|
| 231 | my @blocklist = $from->split($webvar{masklen});
|
---|
| 232 | my $availblocks;
|
---|
| 233 | foreach (@blocklist) {
|
---|
| 234 | $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
|
---|
| 235 | }
|
---|
| 236 | $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
|
---|
| 237 |
|
---|
| 238 | print $html;
|
---|
[65] | 239 | } elsif ($webvar{action} eq 'showpools') {
|
---|
| 240 | print "IP Pools currently allocated:\n".
|
---|
| 241 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
|
---|
[199] | 242 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
|
---|
[65] | 243 | $sth->execute;
|
---|
| 244 | my %poolfree;
|
---|
| 245 | while (my @data = $sth->fetchrow_array) {
|
---|
| 246 | $poolfree{$data[0]} = 0;
|
---|
| 247 | }
|
---|
| 248 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
|
---|
| 249 | $sth->execute;
|
---|
| 250 | while (my @data = $sth->fetchrow_array) {
|
---|
| 251 | $poolfree{$data[0]}++;
|
---|
| 252 | }
|
---|
| 253 | foreach my $key (keys %poolfree) {
|
---|
| 254 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
|
---|
| 255 | "<td>$poolfree{$key}</td></tr>\n";
|
---|
| 256 | }
|
---|
| 257 | print "</table>\n";
|
---|
| 258 | } elsif ($webvar{action} eq 'tweakpool') {
|
---|
| 259 | showPool($webvar{pool});
|
---|
| 260 | } elsif ($webvar{action} eq 'updatepool') {
|
---|
[199] | 261 |
|
---|
[65] | 262 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
|
---|
[199] | 263 | "city='$webvar{city}', type='$webvar{type}', available='".
|
---|
[65] | 264 | (($webvar{available} eq 'y') ? 'y' : 'n').
|
---|
| 265 | "', notes='$webvar{notes}', description='$webvar{desc}' ".
|
---|
| 266 | "where ip='$webvar{ip}'");
|
---|
| 267 | $sth->execute;
|
---|
| 268 | if ($sth->err) {
|
---|
| 269 | print "Error updating pool IP $webvar{ip}: $@<hr>\n";
|
---|
| 270 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
|
---|
| 271 | } else {
|
---|
| 272 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
|
---|
| 273 | $sth->execute;
|
---|
| 274 | my @data = $sth->fetchrow_array;
|
---|
| 275 | print "$webvar{ip} in $data[0] updated\n<hr>\n";
|
---|
| 276 | syslog "notice", "$authuser updated pool IP $webvar{ip}";
|
---|
| 277 | }
|
---|
[258] | 278 | } elsif ($webvar{action} eq 'showusers') {
|
---|
[233] | 279 | print "Notes:<br>\n".
|
---|
[258] | 280 | "<li>Admin users automatically get all other priviledges.\n".
|
---|
| 281 | "<hr>Add new user:<form action=admin.cgi method=POST>\n".
|
---|
| 282 | "Username: <input name=username><br>\n".
|
---|
[259] | 283 | "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
|
---|
[258] | 284 | "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
|
---|
[233] | 285 |
|
---|
| 286 | print "<hr>Users with access:\n<table border=1>\n";
|
---|
[284] | 287 | print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
|
---|
[233] | 288 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
|
---|
[284] | 289 | "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
|
---|
[233] | 290 | "<form action=admin.cgi method=POST>\n";
|
---|
| 291 | $sth = $ip_dbh->prepare("select username,acl from users order by username");
|
---|
| 292 | $sth->execute;
|
---|
| 293 | while (my @data = $sth->fetchrow_array) {
|
---|
| 294 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
|
---|
| 295 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
|
---|
| 296 | # Now for the fun bit. We have to pull apart the ACL field and
|
---|
| 297 | # output a bunch of checkboxes.
|
---|
| 298 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
|
---|
| 299 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
|
---|
| 300 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
|
---|
[284] | 301 | "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
|
---|
[233] | 302 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
|
---|
[258] | 303 | qq(></td><td><input type=submit value="Update"></td></form>\n).
|
---|
| 304 | "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
|
---|
| 305 | "<input type=hidden name=username value=$data[0]>".
|
---|
| 306 | qq(<input type=submit value="Delete user"></tr></form>\n);
|
---|
[233] | 307 |
|
---|
| 308 | }
|
---|
| 309 | print "</table>\n";
|
---|
| 310 | } elsif ($webvar{action} eq 'updacl') {
|
---|
| 311 | print "Updating ACL for $webvar{username}:<br>\n";
|
---|
| 312 | my $acl = 'b';
|
---|
| 313 | if ($webvar{admin} eq 'on') {
|
---|
[284] | 314 | $acl .= "acdsA";
|
---|
[233] | 315 | } else {
|
---|
| 316 | $acl .= ($webvar{add} eq 'on' ? 'a' : '').
|
---|
| 317 | ($webvar{change} eq 'on' ? 'c' : '').
|
---|
[284] | 318 | ($webvar{del} eq 'on' ? 'd' : '').
|
---|
| 319 | ($webvar{sysnet} eq 'on' ? 's' : '');
|
---|
[233] | 320 | }
|
---|
| 321 | print "New ACL: $acl<br>\n";
|
---|
| 322 |
|
---|
| 323 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
|
---|
| 324 | $sth->execute;
|
---|
| 325 | print "OK\n" if !$sth->err;
|
---|
| 326 |
|
---|
[258] | 327 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
[233] | 328 |
|
---|
[258] | 329 | } elsif ($webvar{action} eq 'newuser') {
|
---|
| 330 | print "Adding user $webvar{username}...\n";
|
---|
[259] | 331 | my $cr_pass = ($webvar{preenc} ? $webvar{password} :
|
---|
| 332 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
|
---|
[258] | 333 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
|
---|
| 334 | "('$webvar{username}','$cr_pass','b')");
|
---|
| 335 | $sth->execute;
|
---|
| 336 | if ($sth->err) {
|
---|
| 337 | print "<br>Error adding user: ".$sth->errstr;
|
---|
| 338 | } else {
|
---|
| 339 | print "OK\n";
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
| 343 |
|
---|
| 344 | } elsif ($webvar{action} eq 'deluser') {
|
---|
| 345 | print "Deleting user $webvar{username}.<br>\n";
|
---|
| 346 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
|
---|
| 347 | $sth->execute;
|
---|
| 348 | print "OK\n" if !$sth->err;
|
---|
| 349 |
|
---|
| 350 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
| 351 |
|
---|
[256] | 352 | } elsif ($webvar{action} ne '<NULL>') {
|
---|
[233] | 353 | print "webvar{action} check failed: Don't know how to $webvar{action}";
|
---|
[54] | 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 |
|
---|
[58] | 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 |
|
---|
[54] | 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".
|
---|
[199] | 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".
|
---|
[54] | 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".
|
---|
[199] | 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".
|
---|
[54] | 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; };
|
---|
[65] | 466 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
|
---|
[54] | 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 | }
|
---|
[65] | 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>
|
---|
[199] | 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>
|
---|
[65] | 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";
|
---|
[199] | 502 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
|
---|
[65] | 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 | }
|
---|