source: branches/stable/cgi-bin/admin.cgi@ 412

Last change on this file since 412 was 411, checked in by Kris Deugau, 15 years ago

/branches/stable

Merge extra pieces of admin.cgi from /trunk. See #13.

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