source: trunk/cgi-bin/admin.cgi@ 370

Last change on this file since 370 was 370, checked in by Kris Deugau, 16 years ago

/trunk

Merge bugfixes and enhancements from /branches/stable r351 through r359

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