| 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: 2006-04-07 22:01:00 +0000 (Fri, 07 Apr 2006) $ | 
|---|
| 9 | # SVN revision $Rev: 331 $ | 
|---|
| 10 | # Last update by $Author: kdeugau $ | 
|---|
| 11 | ### | 
|---|
| 12 | # Copyright (C) 2004-2006 - 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 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 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 56 | my %webvar = parse_post(); | 
|---|
| 57 | cleanInput(\%webvar); | 
|---|
| 58 |  | 
|---|
| 59 | print "Content-type: text/html\n\n". | 
|---|
| 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". | 
|---|
| 63 | "<h2>IPDB - Administrative Tools</h2>\n<hr>\n"; | 
|---|
| 64 |  | 
|---|
| 65 | if(!defined($webvar{action})) { | 
|---|
| 66 | $webvar{action} = "<NULL>";   #shuts up the warnings. | 
|---|
| 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 |  | 
|---|
| 77 | my $masterlist = ''; | 
|---|
| 78 | $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr"); | 
|---|
| 79 | $sth->execute; | 
|---|
| 80 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 81 | $masterlist .= "<option value='$data[0]'>$data[0] ($data[1])</option>\n"; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the | 
|---|
| 85 | main interface if at all possible. | 
|---|
| 86 | <hr> | 
|---|
| 87 | <a href="admin.cgi?action=newalloc">Add allocation</a> | 
|---|
| 88 | <hr> | 
|---|
| 89 | <form action="admin.cgi" method="POST"> | 
|---|
| 90 | <input type=hidden name=action value=alloc> | 
|---|
| 91 | Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid> | 
|---|
| 92 | <input type=submit value=" GIMME!! "></form> | 
|---|
| 93 | <hr><form action="admin.cgi" method="POST"> | 
|---|
| 94 | <input type=hidden name=action value=alloctweak> | 
|---|
| 95 | Manually update allocation data in this /24: <input name=allocfrom> | 
|---|
| 96 | <input type=submit value="Show allocations"> | 
|---|
| 97 | </form> | 
|---|
| 98 |  | 
|---|
| 99 | <hr>rWHOIS tools: | 
|---|
| 100 | <form action="admin.cgi" method="POST"> | 
|---|
| 101 | <input type=hidden name=action value=touch> | 
|---|
| 102 | Bump "last updated" timestamp on this master: <select name=whichmaster>$masterlist</select> | 
|---|
| 103 | <input type=submit value="Update timestamp"> (Sets timestamp to "now")</form> | 
|---|
| 104 | <a href="admin.cgi?action=listcust">Edit customer data for rWHOIS</a> | 
|---|
| 105 |  | 
|---|
| 106 | <hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates | 
|---|
| 107 | <hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users;  change | 
|---|
| 108 | internal access controls - note that this does NOT include IP-based limits) | 
|---|
| 109 | <hr>Consistency check tools<br> | 
|---|
| 110 | <a href="consistency-check.pl">General</a>:  Check general netblock consistency.<br> | 
|---|
| 111 | <a href="freespace.pl">Free space</a>:  List total and aggregate free space.  Does not | 
|---|
| 112 | include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8) | 
|---|
| 113 | ); | 
|---|
| 114 | } else { | 
|---|
| 115 | print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>'; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | ## Possible actions. | 
|---|
| 120 | if ($webvar{action} eq 'alloc') { | 
|---|
| 121 | # OK, we know what we're allocating. | 
|---|
| 122 |  | 
|---|
| 123 | if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) { | 
|---|
| 124 | printAndExit("Can't allocate something that's not a netblock/ip"); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'"); | 
|---|
| 128 | $sth->execute; | 
|---|
| 129 | my @data = $sth->fetchrow_array; | 
|---|
| 130 | my $custid = $data[0]; | 
|---|
| 131 | if ($custid eq '') { | 
|---|
| 132 | # Type that doesn't have a default custid | 
|---|
| 133 | $custid = $webvar{custid}; | 
|---|
| 134 | } | 
|---|
| 135 | ##fixme Check billing DB here | 
|---|
| 136 |  | 
|---|
| 137 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
| 138 | my @data; | 
|---|
| 139 | if ($webvar{alloctype} eq 'rm') { | 
|---|
| 140 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'"); | 
|---|
| 141 | $sth->execute; | 
|---|
| 142 | @data = $sth->fetchrow_array; | 
|---|
| 143 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
| 144 | printAndExit("Can't allocate from outside a free block!!\n") | 
|---|
| 145 | if !$data[0]; | 
|---|
| 146 | } elsif ($webvar{alloctype} =~ /^(.)i$/) { | 
|---|
| 147 | $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')"); | 
|---|
| 148 | $sth->execute; | 
|---|
| 149 | @data = $sth->fetchrow_array; | 
|---|
| 150 | # User deserves errors if user can't be bothered to find the pool and a free IP first. | 
|---|
| 151 | printAndExit("Can't allocate static IP from outside a pool!!\n") | 
|---|
| 152 | if !$data[0]; | 
|---|
| 153 | } else { | 
|---|
| 154 | $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')"); | 
|---|
| 155 | $sth->execute; | 
|---|
| 156 | @data = $sth->fetchrow_array; | 
|---|
| 157 | # User deserves errors if user can't be bothered to find the free block first. | 
|---|
| 158 | printAndExit("Can't allocate from outside a routed block!!\n") | 
|---|
| 159 | if !$data[0]; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | my $alloc_from = new NetAddr::IP $data[0]; | 
|---|
| 163 | $sth->finish; | 
|---|
| 164 |  | 
|---|
| 165 | my $cities = ''; | 
|---|
| 166 | foreach my $city (@citylist) { | 
|---|
| 167 | $cities .= "<option>$city</option>\n"; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | print qq(<table class=regular> | 
|---|
| 171 | <form method=POST action=admin.cgi> | 
|---|
| 172 | <tr class=color1> | 
|---|
| 173 | <td>Allocating:</td> | 
|---|
| 174 | <td>$cidr<input type=hidden name=cidr value="$cidr"></td> | 
|---|
| 175 | </tr><tr class=color2> | 
|---|
| 176 | <td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}} | 
|---|
| 177 | <input type=hidden name=alloctype value="$webvar{alloctype}"></td> | 
|---|
| 178 | </tr><tr class=color1> | 
|---|
| 179 | <td>Allocated from:</td> | 
|---|
| 180 | <td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td> | 
|---|
| 181 | </tr><tr class="color2"> | 
|---|
| 182 | <td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td> | 
|---|
| 183 | </tr><tr class=color1> | 
|---|
| 184 | <td>Customer location:</td><td> | 
|---|
| 185 | <select name="city"><option selected>-</option> | 
|---|
| 186 | $cities | 
|---|
| 187 | </select> | 
|---|
| 188 |  <a href="javascript:popNotes('/ip/newcity.html')">Add new location</a> | 
|---|
| 189 | </td> | 
|---|
| 190 | </tr> | 
|---|
| 191 | <tr class="color2"> | 
|---|
| 192 | <td>Circuit ID:</td><td><input name=circid size=40></td> | 
|---|
| 193 | </tr><tr class="color1"> | 
|---|
| 194 | <td>Description/Name:</td><td><input name="desc" size=40></td> | 
|---|
| 195 | </tr><tr class="color2"> | 
|---|
| 196 | <td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td> | 
|---|
| 197 | </tr><tr class="warning"> | 
|---|
| 198 | <td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td> | 
|---|
| 199 | </tr><tr class="color2"> | 
|---|
| 200 | <td class="center" colspan="2"><input type="submit" value="  Assign  "></td> | 
|---|
| 201 | <input type="hidden" name="action" value="confirm"> | 
|---|
| 202 | </form> | 
|---|
| 203 | </tr> | 
|---|
| 204 | </table> | 
|---|
| 205 | ); | 
|---|
| 206 |  | 
|---|
| 207 |  | 
|---|
| 208 | } elsif ($webvar{action} eq 'confirm') { | 
|---|
| 209 |  | 
|---|
| 210 | print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ". | 
|---|
| 211 | "$disp_alloctypes{$webvar{alloctype}}...<br>\n"; | 
|---|
| 212 | # Only need to check city here. | 
|---|
| 213 | if ($webvar{city} eq '-') { | 
|---|
| 214 | printError("Invalid customer location!  Go back and select customer's location."); | 
|---|
| 215 | } else { | 
|---|
| 216 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
| 217 | $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ". | 
|---|
| 218 | "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ". | 
|---|
| 219 | "where ip='$webvar{cidr}'"); | 
|---|
| 220 | $sth->execute; | 
|---|
| 221 | if ($sth->err) { | 
|---|
| 222 | print "Allocation failed!  DBI said:\n".$sth->errstr."\n"; | 
|---|
| 223 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 224 | "'$webvar{alloctype}' failed: '".$sth->errstr."'"; | 
|---|
| 225 | } else { | 
|---|
| 226 | print "Allocation OK!\n"; | 
|---|
| 227 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 228 | "'$webvar{alloctype}'"; | 
|---|
| 229 | # Notify tech@example.com | 
|---|
| 230 | mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
| 231 | "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer". | 
|---|
| 232 | " $webvar{custid}\n". | 
|---|
| 233 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n"); | 
|---|
| 234 | } | 
|---|
| 235 | } else { | 
|---|
| 236 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from}, | 
|---|
| 237 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes}, | 
|---|
| 238 | $webvar{circid}); | 
|---|
| 239 | if ($retcode eq 'OK') { | 
|---|
| 240 | print "Allocation OK!\n"; | 
|---|
| 241 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 242 | "'$webvar{alloctype}'"; | 
|---|
| 243 | } else { | 
|---|
| 244 | print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n"; | 
|---|
| 245 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
| 246 | "'$webvar{alloctype}' failed: '$msg'"; | 
|---|
| 247 | } | 
|---|
| 248 | } # static IP vs netblock | 
|---|
| 249 |  | 
|---|
| 250 | } # done city check | 
|---|
| 251 |  | 
|---|
| 252 | } elsif ($webvar{action} eq 'alloctweak') { | 
|---|
| 253 | fix_allocfrom(); | 
|---|
| 254 | showAllocs($webvar{allocfrom}); | 
|---|
| 255 | } elsif ($webvar{action} eq 'update') { | 
|---|
| 256 | update(); | 
|---|
| 257 | } elsif ($webvar{action} eq 'assign') { | 
|---|
| 258 | # Display a list of possible blocks within the requested block. | 
|---|
| 259 | open (HTML, "../admin_alloc.html") | 
|---|
| 260 | or croak "Could not open admin_alloc.html :$!"; | 
|---|
| 261 | my $html = join('', <HTML>); | 
|---|
| 262 | $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g; | 
|---|
| 263 | $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g; | 
|---|
| 264 |  | 
|---|
| 265 | my $from = new NetAddr::IP $webvar{allocfrom}; | 
|---|
| 266 | my @blocklist = $from->split($webvar{masklen}); | 
|---|
| 267 | my $availblocks; | 
|---|
| 268 | foreach (@blocklist) { | 
|---|
| 269 | $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n); | 
|---|
| 270 | } | 
|---|
| 271 | $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g; | 
|---|
| 272 |  | 
|---|
| 273 | print $html; | 
|---|
| 274 | } elsif ($webvar{action} eq 'touch') { | 
|---|
| 275 | print "Touching master $webvar{whichmaster}\n"; | 
|---|
| 276 | $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'"); | 
|---|
| 277 | $sth->execute; | 
|---|
| 278 | if ($sth->err) { | 
|---|
| 279 | print "<p>Error updating modified timestamp on master $webvar{whichmaster}: ".$sth->errstr."\n"; | 
|---|
| 280 | } | 
|---|
| 281 | } elsif ($webvar{action} eq 'listcust') { | 
|---|
| 282 | print qq(Add new entry:\n | 
|---|
| 283 | <form action=admin.cgi method=POST> | 
|---|
| 284 | <table border=1><tr> | 
|---|
| 285 | <input type=hidden name=action value=newcust> | 
|---|
| 286 | <td>CustID:</td><td><input name=custid></td> | 
|---|
| 287 | <td>Name:</td><td><input name=name></td></tr> | 
|---|
| 288 | <tr><td>Street:</td><td><input name=street></td></tr> | 
|---|
| 289 | <!-- <td>Street2:</td><td><input name=street2></td> --> | 
|---|
| 290 | <tr><td>City:</td><td><input name=city></td> | 
|---|
| 291 | <td>Province: (2-letter code)</td><td><input name=province value=ON length=2 size=2></td></tr> | 
|---|
| 292 | <tr><td>Country: (2-letter code)</td><td><input name=country value=CA length=2 size=2></td> | 
|---|
| 293 | <td>Postal/ZIP Code:</td><td><input name=pocode></td></tr> | 
|---|
| 294 | <tr><td>Phone:</td><td><input name=phone></td> | 
|---|
| 295 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr> | 
|---|
| 296 | <td>Description:</td><td><input name=description></td> --> | 
|---|
| 297 | <td>ARIN Handles:</td><td> | 
|---|
| 298 | Tech: <input name=tech_handle value="VH25-ORG-ARIN"><br> | 
|---|
| 299 | Abuse: <input name=abuse_handle><br> | 
|---|
| 300 | Admin: <input name=admin_handle><br> | 
|---|
| 301 | Note:  Only tech is required at the moment. | 
|---|
| 302 | </td></tr> | 
|---|
| 303 | <tr><td colspan=4 align=center><input type=submit value="Add"></td></tr> | 
|---|
| 304 | </form></table> | 
|---|
| 305 | ); | 
|---|
| 306 | print "<p>Click CustID to edit existing customer contact data:\n". | 
|---|
| 307 | "<table border=1>\n<tr><td>CustID</td><td>Name</td><td>Tech handle</td></tr>\n"; | 
|---|
| 308 | $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid"); | 
|---|
| 309 | $sth->execute; | 
|---|
| 310 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 311 | print qq(<tr><td><a href="admin.cgi?action=edcust&custid=$data[0]">$data[0]</td>). | 
|---|
| 312 | "<td>$data[1]</td><td>$data[2]</td></tr>\n"; | 
|---|
| 313 | } | 
|---|
| 314 | print "</table>\n"; | 
|---|
| 315 | } elsif ($webvar{action} eq 'newcust') { | 
|---|
| 316 | if ($webvar{custid} eq '') { | 
|---|
| 317 | print 'No CustID entered.  PTHBT!  (Hit "Back" and fix the problem.)'; | 
|---|
| 318 | } else { | 
|---|
| 319 | $sth = $ip_dbh->prepare("insert into customers ". | 
|---|
| 320 | "(custid, name, street, city, province, country, pocode, ". | 
|---|
| 321 | "phone, tech_handle, abuse_handle, admin_handle) values ". | 
|---|
| 322 | "('$webvar{custid}', '$webvar{name}', '$webvar{street}', ". | 
|---|
| 323 | "'$webvar{city}', '$webvar{province}', '$webvar{country}', ". | 
|---|
| 324 | "'$webvar{pocode}', '$webvar{phone}', '$webvar{techhandle}', ". | 
|---|
| 325 | "'$webvar{abusehandle}', '$webvar{adminhandle}')"); | 
|---|
| 326 | $sth->execute; | 
|---|
| 327 | if ($sth->err) { | 
|---|
| 328 | print "INSERT failed:  ".$sth->errstr."\n"; | 
|---|
| 329 | } else { | 
|---|
| 330 | print "Success!  Added customer contact data:\n". | 
|---|
| 331 | qq(<table border=1><tr> | 
|---|
| 332 | <td>CustID:</td>$webvar{custid}</td><td>Name:</td>$webvar{name}</td></tr> | 
|---|
| 333 | <tr><td>Street:</td><td>$webvar{street}</td></tr> | 
|---|
| 334 | <tr><td>City:</td><td>$webvar{city}</td><td>Province:</td><td>$webvar{province}</td></tr> | 
|---|
| 335 | <tr><td>Country:</td><td>$webvar{country}</td> | 
|---|
| 336 | <td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr> | 
|---|
| 337 | <tr><td>Phone:</td><td>$webvar{phone}</td> | 
|---|
| 338 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr> | 
|---|
| 339 | <tr><td>Description:</td><td><input name=description></td> --> | 
|---|
| 340 | <td>ARIN Handles:</td><td> | 
|---|
| 341 | Tech: $webvar{tech_handle}<br> | 
|---|
| 342 | Abuse: $webvar{abuse_handle}<br> | 
|---|
| 343 | Admin: $webvar{admin_handle}<br> | 
|---|
| 344 | </td></tr></table> | 
|---|
| 345 | ); | 
|---|
| 346 | } # $sth err check | 
|---|
| 347 | } # bad custid | 
|---|
| 348 | } elsif ($webvar{action} eq 'edcust') { | 
|---|
| 349 | $sth = $ip_dbh->prepare("select custid,name,street,city,province,". | 
|---|
| 350 | "country,pocode,phone,tech_handle,abuse_handle,admin_handle ". | 
|---|
| 351 | "from customers where custid='$webvar{custid}'"); | 
|---|
| 352 | $sth->execute; | 
|---|
| 353 | my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin) = | 
|---|
| 354 | $sth->fetchrow_array; | 
|---|
| 355 | print qq(<form action=admin.cgi method=POST> | 
|---|
| 356 | <table border=1><tr> | 
|---|
| 357 | <input type=hidden name=action value=updcust> | 
|---|
| 358 | <td>CustID:</td><td>$custid<input type=hidden name=custid value=$custid></td> | 
|---|
| 359 | <td>Name:</td><td><input name=name value="$name"></td></tr> | 
|---|
| 360 | <tr><td>Street:</td><td><input name=street value="$street"></td></tr> | 
|---|
| 361 | <!-- <td>Street2:</td><td><input name=street2></td> --> | 
|---|
| 362 | <tr><td>City:</td><td><input name=city value="$city"></td> | 
|---|
| 363 | <td>Province: (2-letter code)</td><td><input name=province value="$prov" length=2 size=2></td></tr> | 
|---|
| 364 | <tr><td>Country: (2-letter code)</td><td><input name=country value="$country" length=2 size=2></td> | 
|---|
| 365 | <td>Postal/ZIP Code:</td><td><input name=pocode value="$pocode"></td></tr> | 
|---|
| 366 | <tr><td>Phone:</td><td><input name=phone value="$pocode"></td> | 
|---|
| 367 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr> | 
|---|
| 368 | <td>Description:</td><td><input name=description></td> --> | 
|---|
| 369 | <td>ARIN Handles:</td><td> | 
|---|
| 370 | Tech: <input name=tech_handle value="$tech"><br> | 
|---|
| 371 | Abuse: <input name=abuse_handle value="$abuse"><br> | 
|---|
| 372 | Admin: <input name=admin_handle value="$admin"><br> | 
|---|
| 373 | Note:  Only tech is required at the moment. | 
|---|
| 374 | </td></tr> | 
|---|
| 375 | <tr><td colspan=4 align=center><input type=submit value="Update"></td></tr> | 
|---|
| 376 | </form></table> | 
|---|
| 377 | ); | 
|---|
| 378 |  | 
|---|
| 379 | } elsif ($webvar{action} eq 'updcust') { | 
|---|
| 380 | print "Updated $webvar{custid}\n"; | 
|---|
| 381 | } elsif ($webvar{action} eq 'showpools') { | 
|---|
| 382 | print "IP Pools currently allocated:\n". | 
|---|
| 383 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n"; | 
|---|
| 384 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr"); | 
|---|
| 385 | $sth->execute; | 
|---|
| 386 | my %poolfree; | 
|---|
| 387 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 388 | $poolfree{$data[0]} = 0; | 
|---|
| 389 | } | 
|---|
| 390 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip"); | 
|---|
| 391 | $sth->execute; | 
|---|
| 392 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 393 | $poolfree{$data[0]}++; | 
|---|
| 394 | } | 
|---|
| 395 | foreach my $key (keys %poolfree) { | 
|---|
| 396 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>). | 
|---|
| 397 | "<td>$poolfree{$key}</td></tr>\n"; | 
|---|
| 398 | } | 
|---|
| 399 | print "</table>\n"; | 
|---|
| 400 | } elsif ($webvar{action} eq 'tweakpool') { | 
|---|
| 401 | showPool($webvar{pool}); | 
|---|
| 402 | } elsif ($webvar{action} eq 'updatepool') { | 
|---|
| 403 |  | 
|---|
| 404 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ". | 
|---|
| 405 | "city='$webvar{city}', type='$webvar{type}', available='". | 
|---|
| 406 | (($webvar{available} eq 'y') ? 'y' : 'n'). | 
|---|
| 407 | "', notes='$webvar{notes}', description='$webvar{desc}' ". | 
|---|
| 408 | "where ip='$webvar{ip}'"); | 
|---|
| 409 | $sth->execute; | 
|---|
| 410 | if ($sth->err) { | 
|---|
| 411 | print "Error updating pool IP $webvar{ip}: $@<hr>\n"; | 
|---|
| 412 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@"; | 
|---|
| 413 | } else { | 
|---|
| 414 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'"); | 
|---|
| 415 | $sth->execute; | 
|---|
| 416 | my @data = $sth->fetchrow_array; | 
|---|
| 417 | print "$webvar{ip} in $data[0] updated\n<hr>\n"; | 
|---|
| 418 | syslog "notice", "$authuser updated pool IP $webvar{ip}"; | 
|---|
| 419 | } | 
|---|
| 420 | } elsif ($webvar{action} eq 'showusers') { | 
|---|
| 421 | print "Notes:<br>\n". | 
|---|
| 422 | "<li>Admin users automatically get all other priviledges.\n". | 
|---|
| 423 | "<li>Everyone has basic read access.\n". | 
|---|
| 424 | "<hr>Add new user:<form action=admin.cgi method=POST>\n". | 
|---|
| 425 | "Username: <input name=username><br>\n". | 
|---|
| 426 | "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n". | 
|---|
| 427 | "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n"; | 
|---|
| 428 |  | 
|---|
| 429 | print "<hr>Users with access:\n<table border=1>\n"; | 
|---|
| 430 | print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n"; | 
|---|
| 431 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>". | 
|---|
| 432 | "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n". | 
|---|
| 433 | "<form action=admin.cgi method=POST>\n"; | 
|---|
| 434 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); | 
|---|
| 435 | $sth->execute; | 
|---|
| 436 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 437 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>". | 
|---|
| 438 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>). | 
|---|
| 439 | # Now for the fun bit.  We have to pull apart the ACL field and | 
|---|
| 440 | # output a bunch of checkboxes. | 
|---|
| 441 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : ''). | 
|---|
| 442 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : ''). | 
|---|
| 443 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : ''). | 
|---|
| 444 | "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : ''). | 
|---|
| 445 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : ''). | 
|---|
| 446 | qq(></td><td><input type=submit value="Update"></td></form>\n). | 
|---|
| 447 | "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>". | 
|---|
| 448 | "<input type=hidden name=username value=$data[0]>". | 
|---|
| 449 | qq(<input type=submit value="Delete user"></tr></form>\n); | 
|---|
| 450 |  | 
|---|
| 451 | } | 
|---|
| 452 | print "</table>\n"; | 
|---|
| 453 | } elsif ($webvar{action} eq 'updacl') { | 
|---|
| 454 | print "Updating ACL for $webvar{username}:<br>\n"; | 
|---|
| 455 | my $acl = 'b'; | 
|---|
| 456 | if ($webvar{admin} eq 'on') { | 
|---|
| 457 | $acl .= "acdsA"; | 
|---|
| 458 | } else { | 
|---|
| 459 | $acl .= ($webvar{add} eq 'on' ? 'a' : ''). | 
|---|
| 460 | ($webvar{change} eq 'on' ? 'c' : ''). | 
|---|
| 461 | ($webvar{del} eq 'on' ? 'd' : ''). | 
|---|
| 462 | ($webvar{sysnet} eq 'on' ? 's' : ''); | 
|---|
| 463 | } | 
|---|
| 464 | print "New ACL: $acl<br>\n"; | 
|---|
| 465 |  | 
|---|
| 466 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'"); | 
|---|
| 467 | $sth->execute; | 
|---|
| 468 | print "OK\n" if !$sth->err; | 
|---|
| 469 |  | 
|---|
| 470 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
| 471 |  | 
|---|
| 472 | } elsif ($webvar{action} eq 'newuser') { | 
|---|
| 473 | print "Adding user $webvar{username}...\n"; | 
|---|
| 474 | my $cr_pass = ($webvar{preenc} ? $webvar{password} : | 
|---|
| 475 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64])); | 
|---|
| 476 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ". | 
|---|
| 477 | "('$webvar{username}','$cr_pass','b')"); | 
|---|
| 478 | $sth->execute; | 
|---|
| 479 | if ($sth->err) { | 
|---|
| 480 | print "<br>Error adding user: ".$sth->errstr; | 
|---|
| 481 | } else { | 
|---|
| 482 | print "OK\n"; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
| 486 |  | 
|---|
| 487 | } elsif ($webvar{action} eq 'deluser') { | 
|---|
| 488 | print "Deleting user $webvar{username}.<br>\n"; | 
|---|
| 489 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'"); | 
|---|
| 490 | $sth->execute; | 
|---|
| 491 | print "OK\n" if !$sth->err; | 
|---|
| 492 |  | 
|---|
| 493 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n); | 
|---|
| 494 |  | 
|---|
| 495 | } elsif ($webvar{action} ne '<NULL>') { | 
|---|
| 496 | print "webvar{action} check failed: Don't know how to $webvar{action}"; | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | # Hokay.  This is a little different.  We have a few specific functions here: | 
|---|
| 500 | #  -> Assign arbitrary subnet from arbitrary free space | 
|---|
| 501 | #  -> Tweak individual DB fields | 
|---|
| 502 | # | 
|---|
| 503 |  | 
|---|
| 504 |  | 
|---|
| 505 | printFooter; | 
|---|
| 506 |  | 
|---|
| 507 | $ip_dbh->disconnect; | 
|---|
| 508 |  | 
|---|
| 509 | exit; | 
|---|
| 510 |  | 
|---|
| 511 |  | 
|---|
| 512 | # Tweak allocfrom into shape. | 
|---|
| 513 | sub fix_allocfrom { | 
|---|
| 514 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) { | 
|---|
| 515 | # 3-octet class C specified | 
|---|
| 516 | $webvar{allocfrom} .= ".0/24"; | 
|---|
| 517 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) { | 
|---|
| 518 | # 4-octet IP specified; | 
|---|
| 519 | $webvar{allocfrom} .= "/24"; | 
|---|
| 520 | } | 
|---|
| 521 | } | 
|---|
| 522 |  | 
|---|
| 523 |  | 
|---|
| 524 | # List free blocks in a /24 for arbitrary manual allocation | 
|---|
| 525 | sub showfree($) { | 
|---|
| 526 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
| 527 | print "Showing free blocks in $cidr<br>\n". | 
|---|
| 528 | "<table border=1>\n"; | 
|---|
| 529 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr"); | 
|---|
| 530 | $sth->execute; | 
|---|
| 531 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 532 | my $temp = new NetAddr::IP $data[0]; | 
|---|
| 533 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n". | 
|---|
| 534 | qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n). | 
|---|
| 535 | "<td>". | 
|---|
| 536 | (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30' | 
|---|
| 537 | : "<select name=masklen><option>30</option>\n<option>29</option>\n") . | 
|---|
| 538 | (($temp->masklen < 29) ? "<option>28</option>\n" : '') . | 
|---|
| 539 | (($temp->masklen < 28) ? "<option>27</option>\n" : '') . | 
|---|
| 540 | (($temp->masklen < 27) ? "<option>26</option>\n" : '') . | 
|---|
| 541 | (($temp->masklen < 26) ? "<option>25</option>\n" : '') . | 
|---|
| 542 | (($temp->masklen < 25) ? "<option>24</option>\n" : '') . | 
|---|
| 543 | "</td>". | 
|---|
| 544 | qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>). | 
|---|
| 545 | "\n</form></tr>\n"; | 
|---|
| 546 | } | 
|---|
| 547 | print "</table>\n"; | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 |  | 
|---|
| 551 | # Show allocations to allow editing. | 
|---|
| 552 | sub showAllocs($) { | 
|---|
| 553 | my $cidr = new NetAddr::IP $_[0]; | 
|---|
| 554 | print "Edit custID, allocation type, city for allocations in ". | 
|---|
| 555 | "$cidr:\n<table border=1>"; | 
|---|
| 556 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr"); | 
|---|
| 557 | $sth->execute; | 
|---|
| 558 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 559 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n". | 
|---|
| 560 | qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n). | 
|---|
| 561 | qq(<td><input name=custid value="$data[1]"></td>\n); | 
|---|
| 562 |  | 
|---|
| 563 | print "<td><select name=alloctype><option". | 
|---|
| 564 | (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option". | 
|---|
| 565 | (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option". | 
|---|
| 566 | (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option". | 
|---|
| 567 | (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option". | 
|---|
| 568 | (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option". | 
|---|
| 569 | (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option". | 
|---|
| 570 | (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option". | 
|---|
| 571 | (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option". | 
|---|
| 572 | (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option". | 
|---|
| 573 | (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option". | 
|---|
| 574 | (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option". | 
|---|
| 575 | (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option". | 
|---|
| 576 | (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". | 
|---|
| 577 | (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option". | 
|---|
| 578 | (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option". | 
|---|
| 579 | (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option". | 
|---|
| 580 | (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". | 
|---|
| 581 | "</select></td>\n"; | 
|---|
| 582 | print qq(<td><input name=city value="$data[3]"></td>\n). | 
|---|
| 583 | "<td>$data[4]</td><td>$data[5]</td>". | 
|---|
| 584 | qq(<td><input type=submit value="Update"></td></form></tr>\n); | 
|---|
| 585 | } | 
|---|
| 586 | print "</table>\n"; | 
|---|
| 587 |  | 
|---|
| 588 | # notes | 
|---|
| 589 | print "<hr><b>Notes:</b>\n". | 
|---|
| 590 | "<ul>\n<li>Use the main interface to update description and notes fields\n". | 
|---|
| 591 | "<li>Changing the allocation type here will NOT affect IP pool data.\n". | 
|---|
| 592 | "</ul>\n"; | 
|---|
| 593 | } | 
|---|
| 594 |  | 
|---|
| 595 |  | 
|---|
| 596 | # Stuff updates into DB | 
|---|
| 597 | sub update { | 
|---|
| 598 | eval { | 
|---|
| 599 | # Relatively simple SQL transaction here.  Note that we're deliberately NOT | 
|---|
| 600 | # updating notes/desc here as it's available through the main interface. | 
|---|
| 601 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',". | 
|---|
| 602 | "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'"); | 
|---|
| 603 | $sth->execute; | 
|---|
| 604 | $ip_dbh->commit; | 
|---|
| 605 | }; | 
|---|
| 606 | if ($@) { | 
|---|
| 607 | carp "Transaction aborted because $@"; | 
|---|
| 608 | eval { $ip_dbh->rollback; }; | 
|---|
| 609 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'"; | 
|---|
| 610 | } else { | 
|---|
| 611 | # If we get here, the operation succeeded. | 
|---|
| 612 | syslog "notice", "$authuser updated $webvar{block}"; | 
|---|
| 613 | print "Allocation $webvar{block} updated<hr>\n"; | 
|---|
| 614 | } | 
|---|
| 615 | # need to get /24 that block is part of | 
|---|
| 616 | my @bits = split /\./, $webvar{block}; | 
|---|
| 617 | $bits[3] = "0/24"; | 
|---|
| 618 | showAllocs((join ".", @bits)); | 
|---|
| 619 | } | 
|---|
| 620 |  | 
|---|
| 621 |  | 
|---|
| 622 | # showPool() | 
|---|
| 623 | # List all IPs in a pool, and allow arbitrary admin changes to each | 
|---|
| 624 | # Allow changes to ALL fields | 
|---|
| 625 | sub showPool($) { | 
|---|
| 626 | my $pool = new NetAddr::IP $_[0]; | 
|---|
| 627 | print qq(Listing pool $pool:\n<table border=1> | 
|---|
| 628 | <form action=admin.cgi method=POST> | 
|---|
| 629 | <input type=hidden name=action value=updatepool> | 
|---|
| 630 | <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr> | 
|---|
| 631 | <tr><td align=right>Customer location:</td><td><input name=city></td></tr> | 
|---|
| 632 | <tr><td align=right>Type:</td><td><select name=type><option selected>-</option> | 
|---|
| 633 | <option value="si">Static IP - Server pool</option> | 
|---|
| 634 | <option value="ci">Static IP - Cable</option> | 
|---|
| 635 | <option value="di">Static IP - DSL</option> | 
|---|
| 636 | <option value="mi">Static IP - Dialup</option> | 
|---|
| 637 | <option value="wi">Static IP - Wireless</option> | 
|---|
| 638 | </select></td></tr> | 
|---|
| 639 | <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr> | 
|---|
| 640 | <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr> | 
|---|
| 641 | <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr> | 
|---|
| 642 | <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr> | 
|---|
| 643 | ). | 
|---|
| 644 | "</table>Update the following record:<table border=1>\n"; | 
|---|
| 645 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip"); | 
|---|
| 646 | $sth->execute; | 
|---|
| 647 | while (my @data = $sth->fetchrow_array) { | 
|---|
| 648 | print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>). | 
|---|
| 649 | "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>". | 
|---|
| 650 | "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n"; | 
|---|
| 651 | } | 
|---|
| 652 | print "</form></table>\n"; | 
|---|
| 653 | } | 
|---|