[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: 2010-05-11 18:56:06 +0000 (Tue, 11 May 2010) $
|
---|
| 9 | # SVN revision $Rev: 398 $
|
---|
| 10 | # Last update by $Author: kdeugau $
|
---|
| 11 | ###
|
---|
[320] | 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);
|
---|
[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".
|
---|
[340] | 50 | "<html><head><title>Access denied</title></head>".
|
---|
| 51 | qq(<body bgcolor="#ffff00" text="#000000" link="#000000" vlink="#000000" alink="#ff0000">\n).
|
---|
[233] | 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".
|
---|
[199] | 61 | "<html>\n<head>\n\t<title>TEST [IPDB admin tools] TEST</title>\n".
|
---|
| 62 | qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
|
---|
[340] | 63 | qq(</head>\n<body bgcolor="#ffff00" text="#000000" link="#000000" vlink="#000000" alink="#ff0000">\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.
|
---|
[199] | 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 |
|
---|
[329] | 78 | my $masterlist = '';
|
---|
| 79 | $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr");
|
---|
| 80 | $sth->execute;
|
---|
| 81 | while (my @data = $sth->fetchrow_array) {
|
---|
| 82 | $masterlist .= "<option value='$data[0]'>$data[0] ($data[1])</option>\n";
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[54] | 85 | print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
|
---|
| 86 | main interface if at all possible.
|
---|
[199] | 87 | <hr>
|
---|
| 88 | <a href="admin.cgi?action=newalloc">Add allocation</a>
|
---|
| 89 | <hr>
|
---|
| 90 | <form action="admin.cgi" method="POST">
|
---|
[54] | 91 | <input type=hidden name=action value=alloc>
|
---|
[199] | 92 | Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
|
---|
| 93 | <input type=submit value=" GIMME!! "></form>
|
---|
[54] | 94 | <hr><form action="admin.cgi" method="POST">
|
---|
| 95 | <input type=hidden name=action value=alloctweak>
|
---|
| 96 | Manually update allocation data in this /24: <input name=allocfrom>
|
---|
| 97 | <input type=submit value="Show allocations">
|
---|
[65] | 98 | </form>
|
---|
[329] | 99 |
|
---|
| 100 | <hr>rWHOIS tools:
|
---|
| 101 | <form action="admin.cgi" method="POST">
|
---|
| 102 | <input type=hidden name=action value=touch>
|
---|
| 103 | Bump "last updated" timestamp on this master: <select name=whichmaster>$masterlist</select>
|
---|
| 104 | <input type=submit value="Update timestamp"> (Sets timestamp to "now")</form>
|
---|
| 105 | <a href="admin.cgi?action=listcust">Edit customer data for rWHOIS</a>
|
---|
| 106 |
|
---|
[65] | 107 | <hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
|
---|
[258] | 108 | <hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users; change
|
---|
| 109 | internal access controls - note that this does NOT include IP-based limits)
|
---|
[320] | 110 | <hr>Consistency check tools<br>
|
---|
| 111 | <a href="consistency-check.pl">General</a>: Check general netblock consistency.<br>
|
---|
| 112 | <a href="freespace.pl">Free space</a>: List total and aggregate free space. Does not
|
---|
| 113 | include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8)
|
---|
[398] | 114 | <hr>(r)WHOIS<br>
|
---|
| 115 | <a href="list-cust.php">List customer data for WHOIS</a> - data used for blocks with the SWIP box checkmarked.
|
---|
| 116 | Links to edit/add data are on this page.
|
---|
[54] | 117 | );
|
---|
| 118 | } else {
|
---|
| 119 | print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[199] | 122 |
|
---|
| 123 | ## Possible actions.
|
---|
[54] | 124 | if ($webvar{action} eq 'alloc') {
|
---|
[199] | 125 | # OK, we know what we're allocating.
|
---|
| 126 |
|
---|
| 127 | if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
|
---|
| 128 | printAndExit("Can't allocate something that's not a netblock/ip");
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
|
---|
| 132 | $sth->execute;
|
---|
| 133 | my @data = $sth->fetchrow_array;
|
---|
| 134 | my $custid = $data[0];
|
---|
| 135 | if ($custid eq '') {
|
---|
| 136 | # Type that doesn't have a default custid
|
---|
| 137 | $custid = $webvar{custid};
|
---|
| 138 | }
|
---|
| 139 | ##fixme Check billing DB here
|
---|
| 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];
|
---|
[256] | 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];
|
---|
[199] | 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">
|
---|
[293] | 206 | </form>
|
---|
[199] | 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 {
|
---|
[293] | 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",
|
---|
[295] | 235 | "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
|
---|
| 236 | " $webvar{custid}\n".
|
---|
[293] | 237 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
| 238 | }
|
---|
| 239 | } else {
|
---|
| 240 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
|
---|
[199] | 241 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
| 242 | $webvar{circid});
|
---|
[293] | 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
|
---|
[199] | 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;
|
---|
[329] | 278 | } elsif ($webvar{action} eq 'touch') {
|
---|
| 279 | print "Touching master $webvar{whichmaster}\n";
|
---|
[330] | 280 | $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
|
---|
| 281 | $sth->execute;
|
---|
| 282 | if ($sth->err) {
|
---|
| 283 | print "<p>Error updating modified timestamp on master $webvar{whichmaster}: ".$sth->errstr."\n";
|
---|
| 284 | }
|
---|
[329] | 285 | } elsif ($webvar{action} eq 'listcust') {
|
---|
| 286 | print qq(Add new entry:\n
|
---|
| 287 | <form action=admin.cgi method=POST>
|
---|
| 288 | <table border=1><tr>
|
---|
| 289 | <input type=hidden name=action value=newcust>
|
---|
| 290 | <td>CustID:</td><td><input name=custid></td>
|
---|
| 291 | <td>Name:</td><td><input name=name></td></tr>
|
---|
| 292 | <tr><td>Street:</td><td><input name=street></td></tr>
|
---|
| 293 | <!-- <td>Street2:</td><td><input name=street2></td> -->
|
---|
| 294 | <tr><td>City:</td><td><input name=city></td>
|
---|
| 295 | <td>Province: (2-letter code)</td><td><input name=province value=ON length=2 size=2></td></tr>
|
---|
| 296 | <tr><td>Country: (2-letter code)</td><td><input name=country value=CA length=2 size=2></td>
|
---|
| 297 | <td>Postal/ZIP Code:</td><td><input name=pocode></td></tr>
|
---|
| 298 | <tr><td>Phone:</td><td><input name=phone></td>
|
---|
| 299 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
|
---|
| 300 | <td>Description:</td><td><input name=description></td> -->
|
---|
| 301 | <td>ARIN Handles:</td><td>
|
---|
| 302 | Tech: <input name=tech_handle value="VH25-ORG-ARIN"><br>
|
---|
| 303 | Abuse: <input name=abuse_handle><br>
|
---|
| 304 | Admin: <input name=admin_handle><br>
|
---|
| 305 | Note: Only tech is required at the moment.
|
---|
| 306 | </td></tr>
|
---|
| 307 | <tr><td colspan=4 align=center><input type=submit value="Add"></td></tr>
|
---|
| 308 | </form></table>
|
---|
| 309 | );
|
---|
[331] | 310 | print "<p>Click CustID to edit existing customer contact data:\n".
|
---|
[329] | 311 | "<table border=1>\n<tr><td>CustID</td><td>Name</td><td>Tech handle</td></tr>\n";
|
---|
| 312 | $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
|
---|
| 313 | $sth->execute;
|
---|
| 314 | while (my @data = $sth->fetchrow_array) {
|
---|
[331] | 315 | print qq(<tr><td><a href="admin.cgi?action=edcust&custid=$data[0]">$data[0]</td>).
|
---|
| 316 | "<td>$data[1]</td><td>$data[2]</td></tr>\n";
|
---|
[329] | 317 | }
|
---|
[331] | 318 | print "</table>\n";
|
---|
[329] | 319 | } elsif ($webvar{action} eq 'newcust') {
|
---|
| 320 | if ($webvar{custid} eq '') {
|
---|
| 321 | print 'No CustID entered. PTHBT! (Hit "Back" and fix the problem.)';
|
---|
| 322 | } else {
|
---|
| 323 | $sth = $ip_dbh->prepare("insert into customers ".
|
---|
| 324 | "(custid, name, street, city, province, country, pocode, ".
|
---|
| 325 | "phone, tech_handle, abuse_handle, admin_handle) values ".
|
---|
| 326 | "('$webvar{custid}', '$webvar{name}', '$webvar{street}', ".
|
---|
| 327 | "'$webvar{city}', '$webvar{province}', '$webvar{country}', ".
|
---|
| 328 | "'$webvar{pocode}', '$webvar{phone}', '$webvar{techhandle}', ".
|
---|
| 329 | "'$webvar{abusehandle}', '$webvar{adminhandle}')");
|
---|
| 330 | $sth->execute;
|
---|
| 331 | if ($sth->err) {
|
---|
| 332 | print "INSERT failed: ".$sth->errstr."\n";
|
---|
| 333 | } else {
|
---|
| 334 | print "Success! Added customer contact data:\n".
|
---|
| 335 | qq(<table border=1><tr>
|
---|
| 336 | <td>CustID:</td>$webvar{custid}</td><td>Name:</td>$webvar{name}</td></tr>
|
---|
| 337 | <tr><td>Street:</td><td>$webvar{street}</td></tr>
|
---|
| 338 | <tr><td>City:</td><td>$webvar{city}</td><td>Province:</td><td>$webvar{province}</td></tr>
|
---|
| 339 | <tr><td>Country:</td><td>$webvar{country}</td>
|
---|
| 340 | <td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr>
|
---|
| 341 | <tr><td>Phone:</td><td>$webvar{phone}</td>
|
---|
| 342 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
|
---|
| 343 | <tr><td>Description:</td><td><input name=description></td> -->
|
---|
| 344 | <td>ARIN Handles:</td><td>
|
---|
| 345 | Tech: $webvar{tech_handle}<br>
|
---|
| 346 | Abuse: $webvar{abuse_handle}<br>
|
---|
| 347 | Admin: $webvar{admin_handle}<br>
|
---|
| 348 | </td></tr></table>
|
---|
| 349 | );
|
---|
| 350 | } # $sth err check
|
---|
| 351 | } # bad custid
|
---|
[331] | 352 | } elsif ($webvar{action} eq 'edcust') {
|
---|
| 353 | $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
|
---|
| 354 | "country,pocode,phone,tech_handle,abuse_handle,admin_handle ".
|
---|
| 355 | "from customers where custid='$webvar{custid}'");
|
---|
| 356 | $sth->execute;
|
---|
| 357 | my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin) =
|
---|
| 358 | $sth->fetchrow_array;
|
---|
| 359 | print qq(<form action=admin.cgi method=POST>
|
---|
| 360 | <table border=1><tr>
|
---|
| 361 | <input type=hidden name=action value=updcust>
|
---|
| 362 | <td>CustID:</td><td>$custid<input type=hidden name=custid value=$custid></td>
|
---|
| 363 | <td>Name:</td><td><input name=name value="$name"></td></tr>
|
---|
| 364 | <tr><td>Street:</td><td><input name=street value="$street"></td></tr>
|
---|
| 365 | <!-- <td>Street2:</td><td><input name=street2></td> -->
|
---|
| 366 | <tr><td>City:</td><td><input name=city value="$city"></td>
|
---|
| 367 | <td>Province: (2-letter code)</td><td><input name=province value="$prov" length=2 size=2></td></tr>
|
---|
| 368 | <tr><td>Country: (2-letter code)</td><td><input name=country value="$country" length=2 size=2></td>
|
---|
| 369 | <td>Postal/ZIP Code:</td><td><input name=pocode value="$pocode"></td></tr>
|
---|
| 370 | <tr><td>Phone:</td><td><input name=phone value="$pocode"></td>
|
---|
| 371 | <!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
|
---|
| 372 | <td>Description:</td><td><input name=description></td> -->
|
---|
| 373 | <td>ARIN Handles:</td><td>
|
---|
| 374 | Tech: <input name=tech_handle value="$tech"><br>
|
---|
| 375 | Abuse: <input name=abuse_handle value="$abuse"><br>
|
---|
| 376 | Admin: <input name=admin_handle value="$admin"><br>
|
---|
| 377 | Note: Only tech is required at the moment.
|
---|
| 378 | </td></tr>
|
---|
| 379 | <tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
|
---|
| 380 | </form></table>
|
---|
| 381 | );
|
---|
| 382 |
|
---|
[329] | 383 | } elsif ($webvar{action} eq 'updcust') {
|
---|
| 384 | print "Updated $webvar{custid}\n";
|
---|
[65] | 385 | } elsif ($webvar{action} eq 'showpools') {
|
---|
| 386 | print "IP Pools currently allocated:\n".
|
---|
| 387 | "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
|
---|
[199] | 388 | $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
|
---|
[65] | 389 | $sth->execute;
|
---|
| 390 | my %poolfree;
|
---|
| 391 | while (my @data = $sth->fetchrow_array) {
|
---|
| 392 | $poolfree{$data[0]} = 0;
|
---|
| 393 | }
|
---|
| 394 | $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
|
---|
| 395 | $sth->execute;
|
---|
| 396 | while (my @data = $sth->fetchrow_array) {
|
---|
| 397 | $poolfree{$data[0]}++;
|
---|
| 398 | }
|
---|
| 399 | foreach my $key (keys %poolfree) {
|
---|
| 400 | print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
|
---|
| 401 | "<td>$poolfree{$key}</td></tr>\n";
|
---|
| 402 | }
|
---|
| 403 | print "</table>\n";
|
---|
| 404 | } elsif ($webvar{action} eq 'tweakpool') {
|
---|
| 405 | showPool($webvar{pool});
|
---|
| 406 | } elsif ($webvar{action} eq 'updatepool') {
|
---|
[199] | 407 |
|
---|
[65] | 408 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
|
---|
[199] | 409 | "city='$webvar{city}', type='$webvar{type}', available='".
|
---|
[65] | 410 | (($webvar{available} eq 'y') ? 'y' : 'n').
|
---|
| 411 | "', notes='$webvar{notes}', description='$webvar{desc}' ".
|
---|
| 412 | "where ip='$webvar{ip}'");
|
---|
| 413 | $sth->execute;
|
---|
| 414 | if ($sth->err) {
|
---|
| 415 | print "Error updating pool IP $webvar{ip}: $@<hr>\n";
|
---|
| 416 | syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
|
---|
| 417 | } else {
|
---|
| 418 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
|
---|
| 419 | $sth->execute;
|
---|
| 420 | my @data = $sth->fetchrow_array;
|
---|
| 421 | print "$webvar{ip} in $data[0] updated\n<hr>\n";
|
---|
| 422 | syslog "notice", "$authuser updated pool IP $webvar{ip}";
|
---|
| 423 | }
|
---|
[258] | 424 | } elsif ($webvar{action} eq 'showusers') {
|
---|
[233] | 425 | print "Notes:<br>\n".
|
---|
[258] | 426 | "<li>Admin users automatically get all other priviledges.\n".
|
---|
[320] | 427 | "<li>Everyone has basic read access.\n".
|
---|
[258] | 428 | "<hr>Add new user:<form action=admin.cgi method=POST>\n".
|
---|
| 429 | "Username: <input name=username><br>\n".
|
---|
[259] | 430 | "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
|
---|
[258] | 431 | "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
|
---|
[233] | 432 |
|
---|
| 433 | print "<hr>Users with access:\n<table border=1>\n";
|
---|
[284] | 434 | print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
|
---|
[233] | 435 | print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
|
---|
[284] | 436 | "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
|
---|
[233] | 437 | "<form action=admin.cgi method=POST>\n";
|
---|
| 438 | $sth = $ip_dbh->prepare("select username,acl from users order by username");
|
---|
| 439 | $sth->execute;
|
---|
| 440 | while (my @data = $sth->fetchrow_array) {
|
---|
| 441 | print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
|
---|
| 442 | qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
|
---|
| 443 | # Now for the fun bit. We have to pull apart the ACL field and
|
---|
| 444 | # output a bunch of checkboxes.
|
---|
| 445 | "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
|
---|
| 446 | "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
|
---|
| 447 | "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
|
---|
[284] | 448 | "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
|
---|
[233] | 449 | "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
|
---|
[258] | 450 | qq(></td><td><input type=submit value="Update"></td></form>\n).
|
---|
| 451 | "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
|
---|
| 452 | "<input type=hidden name=username value=$data[0]>".
|
---|
| 453 | qq(<input type=submit value="Delete user"></tr></form>\n);
|
---|
[233] | 454 |
|
---|
| 455 | }
|
---|
| 456 | print "</table>\n";
|
---|
| 457 | } elsif ($webvar{action} eq 'updacl') {
|
---|
| 458 | print "Updating ACL for $webvar{username}:<br>\n";
|
---|
| 459 | my $acl = 'b';
|
---|
| 460 | if ($webvar{admin} eq 'on') {
|
---|
[284] | 461 | $acl .= "acdsA";
|
---|
[233] | 462 | } else {
|
---|
| 463 | $acl .= ($webvar{add} eq 'on' ? 'a' : '').
|
---|
| 464 | ($webvar{change} eq 'on' ? 'c' : '').
|
---|
[284] | 465 | ($webvar{del} eq 'on' ? 'd' : '').
|
---|
| 466 | ($webvar{sysnet} eq 'on' ? 's' : '');
|
---|
[233] | 467 | }
|
---|
| 468 | print "New ACL: $acl<br>\n";
|
---|
| 469 |
|
---|
| 470 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
|
---|
| 471 | $sth->execute;
|
---|
| 472 | print "OK\n" if !$sth->err;
|
---|
| 473 |
|
---|
[258] | 474 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
[233] | 475 |
|
---|
[258] | 476 | } elsif ($webvar{action} eq 'newuser') {
|
---|
| 477 | print "Adding user $webvar{username}...\n";
|
---|
[259] | 478 | my $cr_pass = ($webvar{preenc} ? $webvar{password} :
|
---|
| 479 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
|
---|
[258] | 480 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
|
---|
| 481 | "('$webvar{username}','$cr_pass','b')");
|
---|
| 482 | $sth->execute;
|
---|
| 483 | if ($sth->err) {
|
---|
| 484 | print "<br>Error adding user: ".$sth->errstr;
|
---|
| 485 | } else {
|
---|
| 486 | print "OK\n";
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
| 490 |
|
---|
| 491 | } elsif ($webvar{action} eq 'deluser') {
|
---|
| 492 | print "Deleting user $webvar{username}.<br>\n";
|
---|
| 493 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
|
---|
| 494 | $sth->execute;
|
---|
| 495 | print "OK\n" if !$sth->err;
|
---|
| 496 |
|
---|
| 497 | print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
|
---|
| 498 |
|
---|
[256] | 499 | } elsif ($webvar{action} ne '<NULL>') {
|
---|
[233] | 500 | print "webvar{action} check failed: Don't know how to $webvar{action}";
|
---|
[54] | 501 | }
|
---|
| 502 |
|
---|
| 503 | # Hokay. This is a little different. We have a few specific functions here:
|
---|
| 504 | # -> Assign arbitrary subnet from arbitrary free space
|
---|
| 505 | # -> Tweak individual DB fields
|
---|
| 506 | #
|
---|
| 507 |
|
---|
| 508 |
|
---|
| 509 | printFooter;
|
---|
| 510 |
|
---|
| 511 | $ip_dbh->disconnect;
|
---|
| 512 |
|
---|
| 513 | exit;
|
---|
| 514 |
|
---|
| 515 |
|
---|
| 516 | # Tweak allocfrom into shape.
|
---|
| 517 | sub fix_allocfrom {
|
---|
| 518 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
|
---|
| 519 | # 3-octet class C specified
|
---|
| 520 | $webvar{allocfrom} .= ".0/24";
|
---|
| 521 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
|
---|
| 522 | # 4-octet IP specified;
|
---|
| 523 | $webvar{allocfrom} .= "/24";
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 |
|
---|
[58] | 528 | # List free blocks in a /24 for arbitrary manual allocation
|
---|
| 529 | sub showfree($) {
|
---|
| 530 | my $cidr = new NetAddr::IP $_[0];
|
---|
| 531 | print "Showing free blocks in $cidr<br>\n".
|
---|
| 532 | "<table border=1>\n";
|
---|
| 533 | $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
|
---|
| 534 | $sth->execute;
|
---|
| 535 | while (my @data = $sth->fetchrow_array) {
|
---|
| 536 | my $temp = new NetAddr::IP $data[0];
|
---|
| 537 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
|
---|
| 538 | qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
|
---|
| 539 | "<td>".
|
---|
| 540 | (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
|
---|
| 541 | : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
|
---|
| 542 | (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
|
---|
| 543 | (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
|
---|
| 544 | (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
|
---|
| 545 | (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
|
---|
| 546 | (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
|
---|
| 547 | "</td>".
|
---|
| 548 | qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
|
---|
| 549 | "\n</form></tr>\n";
|
---|
| 550 | }
|
---|
| 551 | print "</table>\n";
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 |
|
---|
[54] | 555 | # Show allocations to allow editing.
|
---|
| 556 | sub showAllocs($) {
|
---|
| 557 | my $cidr = new NetAddr::IP $_[0];
|
---|
| 558 | print "Edit custID, allocation type, city for allocations in ".
|
---|
| 559 | "$cidr:\n<table border=1>";
|
---|
| 560 | $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
|
---|
| 561 | $sth->execute;
|
---|
| 562 | while (my @data = $sth->fetchrow_array) {
|
---|
| 563 | print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
|
---|
| 564 | qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
|
---|
[348] | 565 | qq(<td><input name=custid value="$data[1]"></td>\n).
|
---|
| 566 | "<td><select name=alloctype>";
|
---|
[54] | 567 |
|
---|
[348] | 568 | my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
|
---|
| 569 | " where listorder < 500 and not (type like '_i') order by listorder");
|
---|
| 570 | $sth2->execute;
|
---|
| 571 | while (my @types = $sth2->fetchrow_array) {
|
---|
| 572 | print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
|
---|
| 573 | " value='$types[0]'>$types[1]</option>\n";
|
---|
| 574 | }
|
---|
| 575 |
|
---|
[54] | 576 | print qq(<td><input name=city value="$data[3]"></td>\n).
|
---|
| 577 | "<td>$data[4]</td><td>$data[5]</td>".
|
---|
| 578 | qq(<td><input type=submit value="Update"></td></form></tr>\n);
|
---|
| 579 | }
|
---|
| 580 | print "</table>\n";
|
---|
| 581 |
|
---|
| 582 | # notes
|
---|
| 583 | print "<hr><b>Notes:</b>\n".
|
---|
| 584 | "<ul>\n<li>Use the main interface to update description and notes fields\n".
|
---|
| 585 | "<li>Changing the allocation type here will NOT affect IP pool data.\n".
|
---|
| 586 | "</ul>\n";
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 |
|
---|
| 590 | # Stuff updates into DB
|
---|
| 591 | sub update {
|
---|
| 592 | eval {
|
---|
| 593 | # Relatively simple SQL transaction here. Note that we're deliberately NOT
|
---|
| 594 | # updating notes/desc here as it's available through the main interface.
|
---|
| 595 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
|
---|
| 596 | "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
|
---|
| 597 | $sth->execute;
|
---|
| 598 | $ip_dbh->commit;
|
---|
| 599 | };
|
---|
| 600 | if ($@) {
|
---|
| 601 | carp "Transaction aborted because $@";
|
---|
| 602 | eval { $ip_dbh->rollback; };
|
---|
[65] | 603 | syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
|
---|
[54] | 604 | } else {
|
---|
| 605 | # If we get here, the operation succeeded.
|
---|
| 606 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
| 607 | print "Allocation $webvar{block} updated<hr>\n";
|
---|
| 608 | }
|
---|
| 609 | # need to get /24 that block is part of
|
---|
| 610 | my @bits = split /\./, $webvar{block};
|
---|
| 611 | $bits[3] = "0/24";
|
---|
| 612 | showAllocs((join ".", @bits));
|
---|
| 613 | }
|
---|
[65] | 614 |
|
---|
| 615 |
|
---|
| 616 | # showPool()
|
---|
| 617 | # List all IPs in a pool, and allow arbitrary admin changes to each
|
---|
| 618 | # Allow changes to ALL fields
|
---|
| 619 | sub showPool($) {
|
---|
| 620 | my $pool = new NetAddr::IP $_[0];
|
---|
| 621 | print qq(Listing pool $pool:\n<table border=1>
|
---|
| 622 | <form action=admin.cgi method=POST>
|
---|
| 623 | <input type=hidden name=action value=updatepool>
|
---|
| 624 | <tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
|
---|
| 625 | <tr><td align=right>Customer location:</td><td><input name=city></td></tr>
|
---|
[348] | 626 | <tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
|
---|
| 627 |
|
---|
| 628 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
|
---|
| 629 | $sth->execute;
|
---|
| 630 | while (my @data = $sth->fetchrow_array) {
|
---|
| 631 | print "<option value='$data[0]'>$data[1]</option>\n";
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | print qq(</select></td></tr>
|
---|
[65] | 635 | <tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
|
---|
| 636 | <tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
|
---|
| 637 | <tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
|
---|
| 638 | <tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
|
---|
| 639 | ).
|
---|
| 640 | "</table>Update the following record:<table border=1>\n";
|
---|
[199] | 641 | $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
|
---|
[65] | 642 | $sth->execute;
|
---|
| 643 | while (my @data = $sth->fetchrow_array) {
|
---|
| 644 | print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
|
---|
| 645 | "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
|
---|
| 646 | "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
|
---|
| 647 | }
|
---|
| 648 | print "</form></table>\n";
|
---|
| 649 | }
|
---|