| [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-10-14 15:16:45 +0000 (Fri, 14 Oct 2005) $ | 
|---|
|  | 9 | # SVN revision $Rev: 293 $ | 
|---|
|  | 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"> | 
|---|
| [293] | 183 | </form> | 
|---|
| [199] | 184 | </tr> | 
|---|
|  | 185 | </table> | 
|---|
|  | 186 | ); | 
|---|
|  | 187 |  | 
|---|
|  | 188 |  | 
|---|
|  | 189 | } elsif ($webvar{action} eq 'confirm') { | 
|---|
|  | 190 |  | 
|---|
|  | 191 | print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ". | 
|---|
|  | 192 | "$disp_alloctypes{$webvar{alloctype}}...<br>\n"; | 
|---|
|  | 193 | # Only need to check city here. | 
|---|
|  | 194 | if ($webvar{city} eq '-') { | 
|---|
|  | 195 | printError("Invalid customer location!  Go back and select customer's location."); | 
|---|
|  | 196 | } else { | 
|---|
| [293] | 197 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 198 | $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ". | 
|---|
|  | 199 | "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ". | 
|---|
|  | 200 | "where ip='$webvar{cidr}'"); | 
|---|
|  | 201 | $sth->execute; | 
|---|
|  | 202 | if ($sth->err) { | 
|---|
|  | 203 | print "Allocation failed!  DBI said:\n".$sth->errstr."\n"; | 
|---|
|  | 204 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 205 | "'$webvar{alloctype}' failed: '".$sth->errstr."'"; | 
|---|
|  | 206 | } else { | 
|---|
|  | 207 | print "Allocation OK!\n"; | 
|---|
|  | 208 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 209 | "'$webvar{alloctype}'"; | 
|---|
|  | 210 | # Notify tech@example.com | 
|---|
|  | 211 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 212 | "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n". | 
|---|
|  | 213 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
|  | 214 | } | 
|---|
|  | 215 | } else { | 
|---|
|  | 216 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from}, | 
|---|
| [199] | 217 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
|  | 218 | $webvar{circid}); | 
|---|
| [293] | 219 | if ($retcode eq 'OK') { | 
|---|
|  | 220 | print "Allocation OK!\n"; | 
|---|
|  | 221 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 222 | "'$webvar{alloctype}'"; | 
|---|
|  | 223 | } else { | 
|---|
|  | 224 | print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n"; | 
|---|
|  | 225 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 226 | "'$webvar{alloctype}' failed: '$msg'"; | 
|---|
|  | 227 | } | 
|---|
|  | 228 | } # static IP vs netblock | 
|---|
| [199] | 229 |  | 
|---|
|  | 230 | } # done city check | 
|---|
|  | 231 |  | 
|---|
| [54] | 232 | } elsif ($webvar{action} eq 'alloctweak') { | 
|---|
|  | 233 | fix_allocfrom(); | 
|---|
|  | 234 | showAllocs($webvar{allocfrom}); | 
|---|
|  | 235 | } elsif ($webvar{action} eq 'update') { | 
|---|
|  | 236 | update(); | 
|---|
| [58] | 237 | } elsif ($webvar{action} eq 'assign') { | 
|---|
|  | 238 | # Display a list of possible blocks within the requested block. | 
|---|
|  | 239 | open (HTML, "../admin_alloc.html") | 
|---|
|  | 240 | or croak "Could not open admin_alloc.html :$!"; | 
|---|
|  | 241 | my $html = join('', <HTML>); | 
|---|
|  | 242 | $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g; | 
|---|
|  | 243 | $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g; | 
|---|
|  | 244 |  | 
|---|
|  | 245 | my $from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
|  | 246 | my @blocklist = $from->split($webvar{masklen}); | 
|---|
|  | 247 | my $availblocks; | 
|---|
|  | 248 | foreach (@blocklist) { | 
|---|
|  | 249 | $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n); | 
|---|
|  | 250 | } | 
|---|
|  | 251 | $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g; | 
|---|
|  | 252 |  | 
|---|
|  | 253 | print $html; | 
|---|
| [65] | 254 | } elsif ($webvar{action} eq 'showpools') { | 
|---|
|  | 255 | print "IP Pools currently allocated:\n". | 
|---|
|  | 256 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n"; | 
|---|
| [199] | 257 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr"); | 
|---|
| [65] | 258 | $sth->execute; | 
|---|
|  | 259 | my %poolfree; | 
|---|
|  | 260 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 261 | $poolfree{$data[0]} = 0; | 
|---|
|  | 262 | } | 
|---|
|  | 263 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip"); | 
|---|
|  | 264 | $sth->execute; | 
|---|
|  | 265 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 266 | $poolfree{$data[0]}++; | 
|---|
|  | 267 | } | 
|---|
|  | 268 | foreach my $key (keys %poolfree) { | 
|---|
|  | 269 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>). | 
|---|
|  | 270 | "<td>$poolfree{$key}</td></tr>\n"; | 
|---|
|  | 271 | } | 
|---|
|  | 272 | print "</table>\n"; | 
|---|
|  | 273 | } elsif ($webvar{action} eq 'tweakpool') { | 
|---|
|  | 274 | showPool($webvar{pool}); | 
|---|
|  | 275 | } elsif ($webvar{action} eq 'updatepool') { | 
|---|
| [199] | 276 |  | 
|---|
| [65] | 277 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ". | 
|---|
| [199] | 278 | "city='$webvar{city}', type='$webvar{type}', available='". | 
|---|
| [65] | 279 | (($webvar{available} eq 'y') ? 'y' : 'n'). | 
|---|
|  | 280 | "', notes='$webvar{notes}', description='$webvar{desc}' ". | 
|---|
|  | 281 | "where ip='$webvar{ip}'"); | 
|---|
|  | 282 | $sth->execute; | 
|---|
|  | 283 | if ($sth->err) { | 
|---|
|  | 284 | print "Error updating pool IP $webvar{ip}: $@<hr>\n"; | 
|---|
|  | 285 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@"; | 
|---|
|  | 286 | } else { | 
|---|
|  | 287 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'"); | 
|---|
|  | 288 | $sth->execute; | 
|---|
|  | 289 | my @data = $sth->fetchrow_array; | 
|---|
|  | 290 | print "$webvar{ip} in $data[0] updated\n<hr>\n"; | 
|---|
|  | 291 | syslog "notice", "$authuser updated pool IP $webvar{ip}"; | 
|---|
|  | 292 | } | 
|---|
| [258] | 293 | } elsif ($webvar{action} eq 'showusers') { | 
|---|
| [233] | 294 | print "Notes:<br>\n". | 
|---|
| [258] | 295 | "<li>Admin users automatically get all other priviledges.\n". | 
|---|
|  | 296 | "<hr>Add new user:<form action=admin.cgi method=POST>\n". | 
|---|
|  | 297 | "Username: <input name=username><br>\n". | 
|---|
| [259] | 298 | "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n". | 
|---|
| [258] | 299 | "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n"; | 
|---|
| [233] | 300 |  | 
|---|
|  | 301 | print "<hr>Users with access:\n<table border=1>\n"; | 
|---|
| [284] | 302 | print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n"; | 
|---|
| [233] | 303 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>". | 
|---|
| [284] | 304 | "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n". | 
|---|
| [233] | 305 | "<form action=admin.cgi method=POST>\n"; | 
|---|
|  | 306 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); | 
|---|
|  | 307 | $sth->execute; | 
|---|
|  | 308 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 309 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>". | 
|---|
|  | 310 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>). | 
|---|
|  | 311 | # Now for the fun bit.  We have to pull apart the ACL field and | 
|---|
|  | 312 | # output a bunch of checkboxes. | 
|---|
|  | 313 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : ''). | 
|---|
|  | 314 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : ''). | 
|---|
|  | 315 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : ''). | 
|---|
| [284] | 316 | "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : ''). | 
|---|
| [233] | 317 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : ''). | 
|---|
| [258] | 318 | qq(></td><td><input type=submit value="Update"></td></form>\n). | 
|---|
|  | 319 | "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>". | 
|---|
|  | 320 | "<input type=hidden name=username value=$data[0]>". | 
|---|
|  | 321 | qq(<input type=submit value="Delete user"></tr></form>\n); | 
|---|
| [233] | 322 |  | 
|---|
|  | 323 | } | 
|---|
|  | 324 | print "</table>\n"; | 
|---|
|  | 325 | } elsif ($webvar{action} eq 'updacl') { | 
|---|
|  | 326 | print "Updating ACL for $webvar{username}:<br>\n"; | 
|---|
|  | 327 | my $acl = 'b'; | 
|---|
|  | 328 | if ($webvar{admin} eq 'on') { | 
|---|
| [284] | 329 | $acl .= "acdsA"; | 
|---|
| [233] | 330 | } else { | 
|---|
|  | 331 | $acl .= ($webvar{add} eq 'on' ? 'a' : ''). | 
|---|
|  | 332 | ($webvar{change} eq 'on' ? 'c' : ''). | 
|---|
| [284] | 333 | ($webvar{del} eq 'on' ? 'd' : ''). | 
|---|
|  | 334 | ($webvar{sysnet} eq 'on' ? 's' : ''); | 
|---|
| [233] | 335 | } | 
|---|
|  | 336 | print "New ACL: $acl<br>\n"; | 
|---|
|  | 337 |  | 
|---|
|  | 338 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'"); | 
|---|
|  | 339 | $sth->execute; | 
|---|
|  | 340 | print "OK\n" if !$sth->err; | 
|---|
|  | 341 |  | 
|---|
| [258] | 342 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
| [233] | 343 |  | 
|---|
| [258] | 344 | } elsif ($webvar{action} eq 'newuser') { | 
|---|
|  | 345 | print "Adding user $webvar{username}...\n"; | 
|---|
| [259] | 346 | my $cr_pass = ($webvar{preenc} ? $webvar{password} : | 
|---|
|  | 347 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64])); | 
|---|
| [258] | 348 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ". | 
|---|
|  | 349 | "('$webvar{username}','$cr_pass','b')"); | 
|---|
|  | 350 | $sth->execute; | 
|---|
|  | 351 | if ($sth->err) { | 
|---|
|  | 352 | print "<br>Error adding user: ".$sth->errstr; | 
|---|
|  | 353 | } else { | 
|---|
|  | 354 | print "OK\n"; | 
|---|
|  | 355 | } | 
|---|
|  | 356 |  | 
|---|
|  | 357 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
|  | 358 |  | 
|---|
|  | 359 | } elsif ($webvar{action} eq 'deluser') { | 
|---|
|  | 360 | print "Deleting user $webvar{username}.<br>\n"; | 
|---|
|  | 361 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'"); | 
|---|
|  | 362 | $sth->execute; | 
|---|
|  | 363 | print "OK\n" if !$sth->err; | 
|---|
|  | 364 |  | 
|---|
|  | 365 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
|  | 366 |  | 
|---|
| [256] | 367 | } elsif ($webvar{action} ne '<NULL>') { | 
|---|
| [233] | 368 | print "webvar{action} check failed: Don't know how to $webvar{action}"; | 
|---|
| [54] | 369 | } | 
|---|
|  | 370 |  | 
|---|
|  | 371 | # Hokay.  This is a little different.  We have a few specific functions here: | 
|---|
|  | 372 | #  -> Assign arbitrary subnet from arbitrary free space | 
|---|
|  | 373 | #  -> Tweak individual DB fields | 
|---|
|  | 374 | # | 
|---|
|  | 375 |  | 
|---|
|  | 376 |  | 
|---|
|  | 377 | printFooter; | 
|---|
|  | 378 |  | 
|---|
|  | 379 | $ip_dbh->disconnect; | 
|---|
|  | 380 |  | 
|---|
|  | 381 | exit; | 
|---|
|  | 382 |  | 
|---|
|  | 383 |  | 
|---|
|  | 384 | # Tweak allocfrom into shape. | 
|---|
|  | 385 | sub fix_allocfrom { | 
|---|
|  | 386 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) { | 
|---|
|  | 387 | # 3-octet class C specified | 
|---|
|  | 388 | $webvar{allocfrom} .= ".0/24"; | 
|---|
|  | 389 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) { | 
|---|
|  | 390 | # 4-octet IP specified; | 
|---|
|  | 391 | $webvar{allocfrom} .= "/24"; | 
|---|
|  | 392 | } | 
|---|
|  | 393 | } | 
|---|
|  | 394 |  | 
|---|
|  | 395 |  | 
|---|
| [58] | 396 | # List free blocks in a /24 for arbitrary manual allocation | 
|---|
|  | 397 | sub showfree($) { | 
|---|
|  | 398 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
|  | 399 | print "Showing free blocks in $cidr<br>\n". | 
|---|
|  | 400 | "<table border=1>\n"; | 
|---|
|  | 401 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr"); | 
|---|
|  | 402 | $sth->execute; | 
|---|
|  | 403 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 404 | my $temp = new NetAddr::IP $data[0]; | 
|---|
|  | 405 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n". | 
|---|
|  | 406 | qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n). | 
|---|
|  | 407 | "<td>". | 
|---|
|  | 408 | (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30' | 
|---|
|  | 409 | : "<select name=masklen><option>30</option>\n<option>29</option>\n") . | 
|---|
|  | 410 | (($temp->masklen < 29) ? "<option>28</option>\n" : '') . | 
|---|
|  | 411 | (($temp->masklen < 28) ? "<option>27</option>\n" : '') . | 
|---|
|  | 412 | (($temp->masklen < 27) ? "<option>26</option>\n" : '') . | 
|---|
|  | 413 | (($temp->masklen < 26) ? "<option>25</option>\n" : '') . | 
|---|
|  | 414 | (($temp->masklen < 25) ? "<option>24</option>\n" : '') . | 
|---|
|  | 415 | "</td>". | 
|---|
|  | 416 | qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>). | 
|---|
|  | 417 | "\n</form></tr>\n"; | 
|---|
|  | 418 | } | 
|---|
|  | 419 | print "</table>\n"; | 
|---|
|  | 420 | } | 
|---|
|  | 421 |  | 
|---|
|  | 422 |  | 
|---|
| [54] | 423 | # Show allocations to allow editing. | 
|---|
|  | 424 | sub showAllocs($) { | 
|---|
|  | 425 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
|  | 426 | print "Edit custID, allocation type, city for allocations in ". | 
|---|
|  | 427 | "$cidr:\n<table border=1>"; | 
|---|
|  | 428 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr"); | 
|---|
|  | 429 | $sth->execute; | 
|---|
|  | 430 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 431 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n". | 
|---|
|  | 432 | qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n). | 
|---|
|  | 433 | qq(<td><input name=custid value="$data[1]"></td>\n); | 
|---|
|  | 434 |  | 
|---|
|  | 435 | print "<td><select name=alloctype><option". | 
|---|
|  | 436 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option". | 
|---|
|  | 437 | (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option". | 
|---|
|  | 438 | (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option". | 
|---|
|  | 439 | (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option". | 
|---|
|  | 440 | (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option". | 
|---|
|  | 441 | (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option". | 
|---|
| [199] | 442 | (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option". | 
|---|
|  | 443 | (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option". | 
|---|
| [54] | 444 | (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option". | 
|---|
|  | 445 | (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option". | 
|---|
|  | 446 | (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option". | 
|---|
| [199] | 447 | (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option". | 
|---|
|  | 448 | (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". | 
|---|
|  | 449 | (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option". | 
|---|
|  | 450 | (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option". | 
|---|
|  | 451 | (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option". | 
|---|
|  | 452 | (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". | 
|---|
| [54] | 453 | "</select></td>\n"; | 
|---|
|  | 454 | print qq(<td><input name=city value="$data[3]"></td>\n). | 
|---|
|  | 455 | "<td>$data[4]</td><td>$data[5]</td>". | 
|---|
|  | 456 | qq(<td><input type=submit value="Update"></td></form></tr>\n); | 
|---|
|  | 457 | } | 
|---|
|  | 458 | print "</table>\n"; | 
|---|
|  | 459 |  | 
|---|
|  | 460 | # notes | 
|---|
|  | 461 | print "<hr><b>Notes:</b>\n". | 
|---|
|  | 462 | "<ul>\n<li>Use the main interface to update description and notes fields\n". | 
|---|
|  | 463 | "<li>Changing the allocation type here will NOT affect IP pool data.\n". | 
|---|
|  | 464 | "</ul>\n"; | 
|---|
|  | 465 | } | 
|---|
|  | 466 |  | 
|---|
|  | 467 |  | 
|---|
|  | 468 | # Stuff updates into DB | 
|---|
|  | 469 | sub update { | 
|---|
|  | 470 | eval { | 
|---|
|  | 471 | # Relatively simple SQL transaction here.  Note that we're deliberately NOT | 
|---|
|  | 472 | # updating notes/desc here as it's available through the main interface. | 
|---|
|  | 473 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',". | 
|---|
|  | 474 | "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'"); | 
|---|
|  | 475 | $sth->execute; | 
|---|
|  | 476 | $ip_dbh->commit; | 
|---|
|  | 477 | }; | 
|---|
|  | 478 | if ($@) { | 
|---|
|  | 479 | carp "Transaction aborted because $@"; | 
|---|
|  | 480 | eval { $ip_dbh->rollback; }; | 
|---|
| [65] | 481 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'"; | 
|---|
| [54] | 482 | } else { | 
|---|
|  | 483 | # If we get here, the operation succeeded. | 
|---|
|  | 484 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
|  | 485 | print "Allocation $webvar{block} updated<hr>\n"; | 
|---|
|  | 486 | } | 
|---|
|  | 487 | # need to get /24 that block is part of | 
|---|
|  | 488 | my @bits = split /\./, $webvar{block}; | 
|---|
|  | 489 | $bits[3] = "0/24"; | 
|---|
|  | 490 | showAllocs((join ".", @bits)); | 
|---|
|  | 491 | } | 
|---|
| [65] | 492 |  | 
|---|
|  | 493 |  | 
|---|
|  | 494 | # showPool() | 
|---|
|  | 495 | # List all IPs in a pool, and allow arbitrary admin changes to each | 
|---|
|  | 496 | # Allow changes to ALL fields | 
|---|
|  | 497 | sub showPool($) { | 
|---|
|  | 498 | my $pool = new NetAddr::IP $_[0]; | 
|---|
|  | 499 | print qq(Listing pool $pool:\n<table border=1> | 
|---|
|  | 500 | <form action=admin.cgi method=POST> | 
|---|
|  | 501 | <input type=hidden name=action value=updatepool> | 
|---|
|  | 502 | <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr> | 
|---|
|  | 503 | <tr><td align=right>Customer location:</td><td><input name=city></td></tr> | 
|---|
| [199] | 504 | <tr><td align=right>Type:</td><td><select name=type><option selected>-</option> | 
|---|
|  | 505 | <option value="si">Static IP - Server pool</option> | 
|---|
|  | 506 | <option value="ci">Static IP - Cable</option> | 
|---|
|  | 507 | <option value="di">Static IP - DSL</option> | 
|---|
|  | 508 | <option value="mi">Static IP - Dialup</option> | 
|---|
|  | 509 | <option value="wi">Static IP - Wireless</option> | 
|---|
| [65] | 510 | </select></td></tr> | 
|---|
|  | 511 | <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr> | 
|---|
|  | 512 | <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr> | 
|---|
|  | 513 | <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr> | 
|---|
|  | 514 | <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr> | 
|---|
|  | 515 | ). | 
|---|
|  | 516 | "</table>Update the following record:<table border=1>\n"; | 
|---|
| [199] | 517 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip"); | 
|---|
| [65] | 518 | $sth->execute; | 
|---|
|  | 519 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 520 | print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>). | 
|---|
|  | 521 | "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>". | 
|---|
|  | 522 | "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n"; | 
|---|
|  | 523 | } | 
|---|
|  | 524 | print "</form></table>\n"; | 
|---|
|  | 525 | } | 
|---|