| [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. | 
|---|
| [200] | 5 | # | 
|---|
| [54] | 6 | ### | 
|---|
|  | 7 | # SVN revision info | 
|---|
|  | 8 | # $Date: 2006-11-08 20:34:48 +0000 (Wed, 08 Nov 2006) $ | 
|---|
|  | 9 | # SVN revision $Rev: 353 $ | 
|---|
|  | 10 | # Last update by $Author: kdeugau $ | 
|---|
|  | 11 | ### | 
|---|
| [319] | 12 | # Copyright (C) 2004-2006 - Kris Deugau | 
|---|
| [54] | 13 |  | 
|---|
|  | 14 | use strict; | 
|---|
|  | 15 | use warnings; | 
|---|
|  | 16 | use CGI::Carp qw(fatalsToBrowser); | 
|---|
|  | 17 | use DBI; | 
|---|
|  | 18 | use CommonWeb qw(:ALL); | 
|---|
| [200] | 19 | use MyIPDB; | 
|---|
|  | 20 | use CustIDCK; | 
|---|
| [54] | 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 |  | 
|---|
| [200] | 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 |  | 
|---|
| [242] | 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 |  | 
|---|
| [54] | 57 | my %webvar = parse_post(); | 
|---|
|  | 58 | cleanInput(\%webvar); | 
|---|
| [58] | 59 |  | 
|---|
| [54] | 60 | print "Content-type: text/html\n\n". | 
|---|
| [242] | 61 | "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n". | 
|---|
| [200] | 62 | qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n). | 
|---|
|  | 63 | "</head>\n<body>\n". | 
|---|
| [54] | 64 | "<h2>IPDB - Administrative Tools</h2>\n<hr>\n"; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | if(!defined($webvar{action})) { | 
|---|
|  | 67 | $webvar{action} = "<NULL>";   #shuts up the warnings. | 
|---|
| [200] | 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 |  | 
|---|
| [54] | 78 | print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the | 
|---|
|  | 79 | main interface if at all possible. | 
|---|
| [200] | 80 | <hr> | 
|---|
|  | 81 | <a href="admin.cgi?action=newalloc">Add allocation</a> | 
|---|
|  | 82 | <hr> | 
|---|
|  | 83 | <form action="admin.cgi" method="POST"> | 
|---|
| [54] | 84 | <input type=hidden name=action value=alloc> | 
|---|
| [200] | 85 | Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid> | 
|---|
|  | 86 | <input type=submit value=" GIMME!! "></form> | 
|---|
| [54] | 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"> | 
|---|
| [200] | 91 | </form> | 
|---|
|  | 92 | <hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates | 
|---|
| [260] | 93 | <hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users;  change | 
|---|
|  | 94 | internal access controls - note that this does NOT include IP-based limits) | 
|---|
| [319] | 95 | <hr>Consistency check tools<br> | 
|---|
|  | 96 | <a href="consistency-check.pl">General</a>:  Check general netblock consistency.<br> | 
|---|
|  | 97 | <a href="freespace.pl">Free space</a>:  List total and aggregate free space.  Does not | 
|---|
|  | 98 | include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8) | 
|---|
| [343] | 99 | <hr>(r)WHOIS<br> | 
|---|
|  | 100 | <a href="list-cust.php">List customer data for WHOIS</a> - data used for blocks with the SWIP box checkmarked. | 
|---|
|  | 101 | Links to edit/add data are on this page. | 
|---|
| [54] | 102 | ); | 
|---|
|  | 103 | } else { | 
|---|
|  | 104 | print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>'; | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
| [200] | 107 |  | 
|---|
|  | 108 | ## Possible actions. | 
|---|
| [54] | 109 | if ($webvar{action} eq 'alloc') { | 
|---|
| [200] | 110 | # OK, we know what we're allocating. | 
|---|
|  | 111 |  | 
|---|
|  | 112 | if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) { | 
|---|
|  | 113 | printAndExit("Can't allocate something that's not a netblock/ip"); | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'"); | 
|---|
|  | 117 | $sth->execute; | 
|---|
|  | 118 | my @data = $sth->fetchrow_array; | 
|---|
|  | 119 | my $custid = $data[0]; | 
|---|
|  | 120 | if ($custid eq '') { | 
|---|
|  | 121 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) { | 
|---|
|  | 122 | # Force uppercase for now... | 
|---|
|  | 123 | $webvar{custid} =~ tr/a-z/A-Z/; | 
|---|
| [338] | 124 | # Crosscheck with billing. | 
|---|
| [200] | 125 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
|  | 126 | if ($CustIDCK::Error) { | 
|---|
|  | 127 | printError("Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
|  | 128 | return; | 
|---|
|  | 129 | } | 
|---|
|  | 130 | if (!$status) { | 
|---|
|  | 131 | printError("Customer ID not valid.  Make sure the Customer ID ". | 
|---|
|  | 132 | "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ". | 
|---|
|  | 133 | "non-customer assignments."); | 
|---|
|  | 134 | return; | 
|---|
|  | 135 | } | 
|---|
|  | 136 | } | 
|---|
|  | 137 | # Type that doesn't have a default custid | 
|---|
|  | 138 | $custid = $webvar{custid}; | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
|  | 142 | my @data; | 
|---|
|  | 143 | if ($webvar{alloctype} eq 'rm') { | 
|---|
|  | 144 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'"); | 
|---|
|  | 145 | $sth->execute; | 
|---|
|  | 146 | @data = $sth->fetchrow_array; | 
|---|
|  | 147 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
|  | 148 | printAndExit("Can't allocate from outside a free block!!\n") | 
|---|
|  | 149 | if !$data[0]; | 
|---|
| [252] | 150 | } elsif ($webvar{alloctype} =~ /^(.)i$/) { | 
|---|
|  | 151 | $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')"); | 
|---|
|  | 152 | $sth->execute; | 
|---|
|  | 153 | @data = $sth->fetchrow_array; | 
|---|
|  | 154 | # User deserves errors if user can't be bothered to find the pool and a free IP first. | 
|---|
|  | 155 | printAndExit("Can't allocate static IP from outside a pool!!\n") | 
|---|
|  | 156 | if !$data[0]; | 
|---|
| [200] | 157 | } else { | 
|---|
|  | 158 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')"); | 
|---|
|  | 159 | $sth->execute; | 
|---|
|  | 160 | @data = $sth->fetchrow_array; | 
|---|
|  | 161 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
|  | 162 | printAndExit("Can't allocate from outside a routed block!!\n") | 
|---|
|  | 163 | if !$data[0]; | 
|---|
|  | 164 | } | 
|---|
|  | 165 |  | 
|---|
|  | 166 | my $alloc_from = new NetAddr::IP $data[0]; | 
|---|
|  | 167 | $sth->finish; | 
|---|
|  | 168 |  | 
|---|
|  | 169 | my $cities = ''; | 
|---|
|  | 170 | foreach my $city (@citylist) { | 
|---|
|  | 171 | $cities .= "<option>$city</option>\n"; | 
|---|
|  | 172 | } | 
|---|
|  | 173 |  | 
|---|
|  | 174 | print qq(<table class=regular> | 
|---|
|  | 175 | <form method=POST action=admin.cgi> | 
|---|
|  | 176 | <tr class=color1> | 
|---|
|  | 177 | <td>Allocating:</td> | 
|---|
|  | 178 | <td>$cidr<input type=hidden name=cidr value="$cidr"></td> | 
|---|
|  | 179 | </tr><tr class=color2> | 
|---|
|  | 180 | <td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}} | 
|---|
|  | 181 | <input type=hidden name=alloctype value="$webvar{alloctype}"></td> | 
|---|
|  | 182 | </tr><tr class=color1> | 
|---|
|  | 183 | <td>Allocated from:</td> | 
|---|
|  | 184 | <td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td> | 
|---|
|  | 185 | </tr><tr class="color2"> | 
|---|
|  | 186 | <td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td> | 
|---|
|  | 187 | </tr><tr class=color1> | 
|---|
|  | 188 | <td>Customer location:</td><td> | 
|---|
|  | 189 | <select name="city"><option selected>-</option> | 
|---|
|  | 190 | $cities | 
|---|
|  | 191 | </select> | 
|---|
|  | 192 |  <a href="javascript:popNotes('/ip/newcity.html')">Add new location</a> | 
|---|
|  | 193 | </td> | 
|---|
|  | 194 | </tr> | 
|---|
|  | 195 | <tr class="color2"> | 
|---|
|  | 196 | <td>Circuit ID:</td><td><input name=circid size=40></td> | 
|---|
|  | 197 | </tr><tr class="color1"> | 
|---|
|  | 198 | <td>Description/Name:</td><td><input name="desc" size=40></td> | 
|---|
|  | 199 | </tr><tr class="color2"> | 
|---|
|  | 200 | <td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td> | 
|---|
|  | 201 | </tr><tr class="warning"> | 
|---|
|  | 202 | <td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td> | 
|---|
|  | 203 | </tr><tr class="color2"> | 
|---|
|  | 204 | <td class="center" colspan="2"><input type="submit" value="  Assign  "></td> | 
|---|
|  | 205 | <input type="hidden" name="action" value="confirm"> | 
|---|
| [292] | 206 | </form> | 
|---|
| [200] | 207 | </tr> | 
|---|
|  | 208 | </table> | 
|---|
|  | 209 | ); | 
|---|
|  | 210 |  | 
|---|
|  | 211 |  | 
|---|
|  | 212 | } elsif ($webvar{action} eq 'confirm') { | 
|---|
|  | 213 |  | 
|---|
|  | 214 | print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ". | 
|---|
|  | 215 | "$disp_alloctypes{$webvar{alloctype}}...<br>\n"; | 
|---|
|  | 216 | # Only need to check city here. | 
|---|
|  | 217 | if ($webvar{city} eq '-') { | 
|---|
|  | 218 | printError("Invalid customer location!  Go back and select customer's location."); | 
|---|
|  | 219 | } else { | 
|---|
| [292] | 220 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 221 | $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ". | 
|---|
|  | 222 | "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ". | 
|---|
|  | 223 | "where ip='$webvar{cidr}'"); | 
|---|
|  | 224 | $sth->execute; | 
|---|
|  | 225 | if ($sth->err) { | 
|---|
|  | 226 | print "Allocation failed!  DBI said:\n".$sth->errstr."\n"; | 
|---|
|  | 227 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 228 | "'$webvar{alloctype}' failed: '".$sth->errstr."'"; | 
|---|
|  | 229 | } else { | 
|---|
|  | 230 | print "Allocation OK!\n"; | 
|---|
|  | 231 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 232 | "'$webvar{alloctype}'"; | 
|---|
|  | 233 | # Notify tech@example.com | 
|---|
|  | 234 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
| [294] | 235 | "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer". | 
|---|
|  | 236 | " $webvar{custid}\n". | 
|---|
| [292] | 237 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
|  | 238 | } | 
|---|
|  | 239 | } else { | 
|---|
|  | 240 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from}, | 
|---|
| [200] | 241 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
|  | 242 | $webvar{circid}); | 
|---|
| [292] | 243 | if ($retcode eq 'OK') { | 
|---|
|  | 244 | print "Allocation OK!\n"; | 
|---|
|  | 245 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 246 | "'$webvar{alloctype}'"; | 
|---|
|  | 247 | } else { | 
|---|
|  | 248 | print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n"; | 
|---|
|  | 249 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 250 | "'$webvar{alloctype}' failed: '$msg'"; | 
|---|
|  | 251 | } | 
|---|
|  | 252 | } # static IP vs netblock | 
|---|
| [200] | 253 |  | 
|---|
|  | 254 | } # done city check | 
|---|
|  | 255 |  | 
|---|
| [54] | 256 | } elsif ($webvar{action} eq 'alloctweak') { | 
|---|
|  | 257 | fix_allocfrom(); | 
|---|
|  | 258 | showAllocs($webvar{allocfrom}); | 
|---|
|  | 259 | } elsif ($webvar{action} eq 'update') { | 
|---|
|  | 260 | update(); | 
|---|
| [58] | 261 | } elsif ($webvar{action} eq 'assign') { | 
|---|
|  | 262 | # Display a list of possible blocks within the requested block. | 
|---|
|  | 263 | open (HTML, "../admin_alloc.html") | 
|---|
|  | 264 | or croak "Could not open admin_alloc.html :$!"; | 
|---|
|  | 265 | my $html = join('', <HTML>); | 
|---|
|  | 266 | $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g; | 
|---|
|  | 267 | $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g; | 
|---|
|  | 268 |  | 
|---|
|  | 269 | my $from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
|  | 270 | my @blocklist = $from->split($webvar{masklen}); | 
|---|
|  | 271 | my $availblocks; | 
|---|
|  | 272 | foreach (@blocklist) { | 
|---|
|  | 273 | $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n); | 
|---|
|  | 274 | } | 
|---|
|  | 275 | $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g; | 
|---|
|  | 276 |  | 
|---|
|  | 277 | print $html; | 
|---|
| [200] | 278 | } elsif ($webvar{action} eq 'showpools') { | 
|---|
|  | 279 | print "IP Pools currently allocated:\n". | 
|---|
|  | 280 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n"; | 
|---|
|  | 281 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr"); | 
|---|
|  | 282 | $sth->execute; | 
|---|
|  | 283 | my %poolfree; | 
|---|
|  | 284 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 285 | $poolfree{$data[0]} = 0; | 
|---|
|  | 286 | } | 
|---|
|  | 287 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip"); | 
|---|
|  | 288 | $sth->execute; | 
|---|
|  | 289 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 290 | $poolfree{$data[0]}++; | 
|---|
|  | 291 | } | 
|---|
|  | 292 | foreach my $key (keys %poolfree) { | 
|---|
|  | 293 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>). | 
|---|
|  | 294 | "<td>$poolfree{$key}</td></tr>\n"; | 
|---|
|  | 295 | } | 
|---|
|  | 296 | print "</table>\n"; | 
|---|
|  | 297 | } elsif ($webvar{action} eq 'tweakpool') { | 
|---|
|  | 298 | showPool($webvar{pool}); | 
|---|
|  | 299 | } elsif ($webvar{action} eq 'updatepool') { | 
|---|
|  | 300 |  | 
|---|
|  | 301 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ". | 
|---|
|  | 302 | "city='$webvar{city}', type='$webvar{type}', available='". | 
|---|
|  | 303 | (($webvar{available} eq 'y') ? 'y' : 'n'). | 
|---|
|  | 304 | "', notes='$webvar{notes}', description='$webvar{desc}' ". | 
|---|
|  | 305 | "where ip='$webvar{ip}'"); | 
|---|
|  | 306 | $sth->execute; | 
|---|
|  | 307 | if ($sth->err) { | 
|---|
|  | 308 | print "Error updating pool IP $webvar{ip}: $@<hr>\n"; | 
|---|
|  | 309 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@"; | 
|---|
|  | 310 | } else { | 
|---|
|  | 311 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'"); | 
|---|
|  | 312 | $sth->execute; | 
|---|
|  | 313 | my @data = $sth->fetchrow_array; | 
|---|
|  | 314 | print "$webvar{ip} in $data[0] updated\n<hr>\n"; | 
|---|
|  | 315 | syslog "notice", "$authuser updated pool IP $webvar{ip}"; | 
|---|
|  | 316 | } | 
|---|
| [260] | 317 | } elsif ($webvar{action} eq 'showusers') { | 
|---|
| [242] | 318 | print "Notes:<br>\n". | 
|---|
| [260] | 319 | "<li>Admin users automatically get all other priviledges.\n". | 
|---|
| [319] | 320 | "<li>Everyone has basic read access.\n". | 
|---|
| [260] | 321 | "<hr>Add new user:<form action=admin.cgi method=POST>\n". | 
|---|
|  | 322 | "Username: <input name=username><br>\n". | 
|---|
|  | 323 | "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n". | 
|---|
|  | 324 | "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n"; | 
|---|
| [242] | 325 |  | 
|---|
|  | 326 | print "<hr>Users with access:\n<table border=1>\n"; | 
|---|
| [286] | 327 | print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n"; | 
|---|
| [242] | 328 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>". | 
|---|
| [286] | 329 | "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n". | 
|---|
| [242] | 330 | "<form action=admin.cgi method=POST>\n"; | 
|---|
|  | 331 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); | 
|---|
|  | 332 | $sth->execute; | 
|---|
|  | 333 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 334 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>". | 
|---|
|  | 335 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>). | 
|---|
|  | 336 | # Now for the fun bit.  We have to pull apart the ACL field and | 
|---|
|  | 337 | # output a bunch of checkboxes. | 
|---|
|  | 338 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : ''). | 
|---|
|  | 339 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : ''). | 
|---|
|  | 340 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : ''). | 
|---|
| [286] | 341 | "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : ''). | 
|---|
| [242] | 342 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : ''). | 
|---|
| [260] | 343 | qq(></td><td><input type=submit value="Update"></td></form>\n). | 
|---|
|  | 344 | "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>". | 
|---|
|  | 345 | "<input type=hidden name=username value=$data[0]>". | 
|---|
|  | 346 | qq(<input type=submit value="Delete user"></tr></form>\n); | 
|---|
| [242] | 347 |  | 
|---|
|  | 348 | } | 
|---|
|  | 349 | print "</table>\n"; | 
|---|
|  | 350 | } elsif ($webvar{action} eq 'updacl') { | 
|---|
|  | 351 | print "Updating ACL for $webvar{username}:<br>\n"; | 
|---|
|  | 352 | my $acl = 'b'; | 
|---|
|  | 353 | if ($webvar{admin} eq 'on') { | 
|---|
| [286] | 354 | $acl .= "acdsA"; | 
|---|
| [242] | 355 | } else { | 
|---|
|  | 356 | $acl .= ($webvar{add} eq 'on' ? 'a' : ''). | 
|---|
|  | 357 | ($webvar{change} eq 'on' ? 'c' : ''). | 
|---|
| [286] | 358 | ($webvar{del} eq 'on' ? 'd' : ''). | 
|---|
|  | 359 | ($webvar{sysnet} eq 'on' ? 's' : ''); | 
|---|
| [242] | 360 | } | 
|---|
|  | 361 | print "New ACL: $acl<br>\n"; | 
|---|
|  | 362 |  | 
|---|
|  | 363 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'"); | 
|---|
|  | 364 | $sth->execute; | 
|---|
|  | 365 | print "OK\n" if !$sth->err; | 
|---|
|  | 366 |  | 
|---|
| [260] | 367 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
| [242] | 368 |  | 
|---|
| [260] | 369 | } elsif ($webvar{action} eq 'newuser') { | 
|---|
|  | 370 | print "Adding user $webvar{username}...\n"; | 
|---|
|  | 371 | my $cr_pass = ($webvar{preenc} ? $webvar{password} : | 
|---|
|  | 372 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64])); | 
|---|
|  | 373 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ". | 
|---|
|  | 374 | "('$webvar{username}','$cr_pass','b')"); | 
|---|
|  | 375 | $sth->execute; | 
|---|
|  | 376 | if ($sth->err) { | 
|---|
|  | 377 | print "<br>Error adding user: ".$sth->errstr; | 
|---|
|  | 378 | } else { | 
|---|
|  | 379 | print "OK\n"; | 
|---|
|  | 380 | } | 
|---|
|  | 381 |  | 
|---|
|  | 382 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
|  | 383 |  | 
|---|
|  | 384 | } elsif ($webvar{action} eq 'deluser') { | 
|---|
|  | 385 | print "Deleting user $webvar{username}.<br>\n"; | 
|---|
|  | 386 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'"); | 
|---|
|  | 387 | $sth->execute; | 
|---|
|  | 388 | print "OK\n" if !$sth->err; | 
|---|
|  | 389 |  | 
|---|
|  | 390 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
|  | 391 |  | 
|---|
| [242] | 392 | } elsif ($webvar{action} ne '<NULL>') { | 
|---|
|  | 393 | print "webvar{action} check failed: Don't know how to $webvar{action}"; | 
|---|
| [54] | 394 | } | 
|---|
|  | 395 |  | 
|---|
|  | 396 | # Hokay.  This is a little different.  We have a few specific functions here: | 
|---|
|  | 397 | #  -> Assign arbitrary subnet from arbitrary free space | 
|---|
|  | 398 | #  -> Tweak individual DB fields | 
|---|
|  | 399 | # | 
|---|
|  | 400 |  | 
|---|
| [335] | 401 | print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n); | 
|---|
| [54] | 402 |  | 
|---|
|  | 403 | printFooter; | 
|---|
|  | 404 |  | 
|---|
|  | 405 | $ip_dbh->disconnect; | 
|---|
|  | 406 |  | 
|---|
|  | 407 | exit; | 
|---|
|  | 408 |  | 
|---|
|  | 409 |  | 
|---|
|  | 410 | # Tweak allocfrom into shape. | 
|---|
|  | 411 | sub fix_allocfrom { | 
|---|
|  | 412 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) { | 
|---|
|  | 413 | # 3-octet class C specified | 
|---|
|  | 414 | $webvar{allocfrom} .= ".0/24"; | 
|---|
|  | 415 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) { | 
|---|
|  | 416 | # 4-octet IP specified; | 
|---|
|  | 417 | $webvar{allocfrom} .= "/24"; | 
|---|
|  | 418 | } | 
|---|
|  | 419 | } | 
|---|
|  | 420 |  | 
|---|
|  | 421 |  | 
|---|
| [58] | 422 | # List free blocks in a /24 for arbitrary manual allocation | 
|---|
|  | 423 | sub showfree($) { | 
|---|
|  | 424 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
|  | 425 | print "Showing free blocks in $cidr<br>\n". | 
|---|
|  | 426 | "<table border=1>\n"; | 
|---|
|  | 427 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr"); | 
|---|
|  | 428 | $sth->execute; | 
|---|
|  | 429 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 430 | my $temp = new NetAddr::IP $data[0]; | 
|---|
|  | 431 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n". | 
|---|
|  | 432 | qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n). | 
|---|
|  | 433 | "<td>". | 
|---|
|  | 434 | (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30' | 
|---|
|  | 435 | : "<select name=masklen><option>30</option>\n<option>29</option>\n") . | 
|---|
|  | 436 | (($temp->masklen < 29) ? "<option>28</option>\n" : '') . | 
|---|
|  | 437 | (($temp->masklen < 28) ? "<option>27</option>\n" : '') . | 
|---|
|  | 438 | (($temp->masklen < 27) ? "<option>26</option>\n" : '') . | 
|---|
|  | 439 | (($temp->masklen < 26) ? "<option>25</option>\n" : '') . | 
|---|
|  | 440 | (($temp->masklen < 25) ? "<option>24</option>\n" : '') . | 
|---|
|  | 441 | "</td>". | 
|---|
|  | 442 | qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>). | 
|---|
|  | 443 | "\n</form></tr>\n"; | 
|---|
|  | 444 | } | 
|---|
|  | 445 | print "</table>\n"; | 
|---|
|  | 446 | } | 
|---|
|  | 447 |  | 
|---|
|  | 448 |  | 
|---|
| [54] | 449 | # Show allocations to allow editing. | 
|---|
|  | 450 | sub showAllocs($) { | 
|---|
|  | 451 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
|  | 452 | print "Edit custID, allocation type, city for allocations in ". | 
|---|
|  | 453 | "$cidr:\n<table border=1>"; | 
|---|
|  | 454 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr"); | 
|---|
|  | 455 | $sth->execute; | 
|---|
|  | 456 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 457 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n". | 
|---|
|  | 458 | qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n). | 
|---|
| [269] | 459 | qq(<td><input name=custid value="$data[1]"></td>\n). | 
|---|
|  | 460 | "<td><select name=alloctype>"; | 
|---|
| [54] | 461 |  | 
|---|
| [269] | 462 | my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes". | 
|---|
|  | 463 | " where listorder < 500 and not (type like '_i') order by listorder"); | 
|---|
|  | 464 | $sth2->execute; | 
|---|
|  | 465 | while (my @types = $sth2->fetchrow_array) { | 
|---|
|  | 466 | print "<option". (($data[2] eq $types[0]) ? ' selected' : '') . | 
|---|
|  | 467 | " value='$types[0]'>$types[1]</option>\n"; | 
|---|
|  | 468 | } | 
|---|
|  | 469 |  | 
|---|
| [54] | 470 | print qq(<td><input name=city value="$data[3]"></td>\n). | 
|---|
|  | 471 | "<td>$data[4]</td><td>$data[5]</td>". | 
|---|
|  | 472 | qq(<td><input type=submit value="Update"></td></form></tr>\n); | 
|---|
|  | 473 | } | 
|---|
|  | 474 | print "</table>\n"; | 
|---|
|  | 475 |  | 
|---|
|  | 476 | # notes | 
|---|
|  | 477 | print "<hr><b>Notes:</b>\n". | 
|---|
|  | 478 | "<ul>\n<li>Use the main interface to update description and notes fields\n". | 
|---|
|  | 479 | "<li>Changing the allocation type here will NOT affect IP pool data.\n". | 
|---|
|  | 480 | "</ul>\n"; | 
|---|
|  | 481 | } | 
|---|
|  | 482 |  | 
|---|
|  | 483 |  | 
|---|
|  | 484 | # Stuff updates into DB | 
|---|
|  | 485 | sub update { | 
|---|
|  | 486 | eval { | 
|---|
|  | 487 | # Relatively simple SQL transaction here.  Note that we're deliberately NOT | 
|---|
|  | 488 | # updating notes/desc here as it's available through the main interface. | 
|---|
|  | 489 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',". | 
|---|
|  | 490 | "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'"); | 
|---|
|  | 491 | $sth->execute; | 
|---|
|  | 492 | $ip_dbh->commit; | 
|---|
|  | 493 | }; | 
|---|
|  | 494 | if ($@) { | 
|---|
|  | 495 | carp "Transaction aborted because $@"; | 
|---|
|  | 496 | eval { $ip_dbh->rollback; }; | 
|---|
| [200] | 497 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'"; | 
|---|
| [54] | 498 | } else { | 
|---|
|  | 499 | # If we get here, the operation succeeded. | 
|---|
|  | 500 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
|  | 501 | print "Allocation $webvar{block} updated<hr>\n"; | 
|---|
|  | 502 | } | 
|---|
|  | 503 | # need to get /24 that block is part of | 
|---|
|  | 504 | my @bits = split /\./, $webvar{block}; | 
|---|
|  | 505 | $bits[3] = "0/24"; | 
|---|
|  | 506 | showAllocs((join ".", @bits)); | 
|---|
|  | 507 | } | 
|---|
| [200] | 508 |  | 
|---|
|  | 509 |  | 
|---|
|  | 510 | # showPool() | 
|---|
|  | 511 | # List all IPs in a pool, and allow arbitrary admin changes to each | 
|---|
|  | 512 | # Allow changes to ALL fields | 
|---|
|  | 513 | sub showPool($) { | 
|---|
|  | 514 | my $pool = new NetAddr::IP $_[0]; | 
|---|
|  | 515 | print qq(Listing pool $pool:\n<table border=1> | 
|---|
|  | 516 | <form action=admin.cgi method=POST> | 
|---|
|  | 517 | <input type=hidden name=action value=updatepool> | 
|---|
|  | 518 | <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr> | 
|---|
|  | 519 | <tr><td align=right>Customer location:</td><td><input name=city></td></tr> | 
|---|
| [269] | 520 | <tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n); | 
|---|
|  | 521 |  | 
|---|
|  | 522 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder"); | 
|---|
|  | 523 | $sth->execute; | 
|---|
|  | 524 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 525 | print "<option value='$data[0]'>$data[1]</option>\n"; | 
|---|
|  | 526 | } | 
|---|
|  | 527 |  | 
|---|
|  | 528 | print qq(</select></td></tr> | 
|---|
| [200] | 529 | <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr> | 
|---|
|  | 530 | <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr> | 
|---|
|  | 531 | <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr> | 
|---|
|  | 532 | <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr> | 
|---|
|  | 533 | ). | 
|---|
|  | 534 | "</table>Update the following record:<table border=1>\n"; | 
|---|
|  | 535 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip"); | 
|---|
|  | 536 | $sth->execute; | 
|---|
|  | 537 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 538 | print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>). | 
|---|
|  | 539 | "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>". | 
|---|
|  | 540 | "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n"; | 
|---|
|  | 541 | } | 
|---|
|  | 542 | print "</form></table>\n"; | 
|---|
|  | 543 | } | 
|---|