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

Last change on this file since 330 was 330, checked in by Kris Deugau, 18 years ago

/trunk

Added supporting code to update masterblock modified timestamp
Removed debugging fragment

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 24.4 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: 2006-04-07 17:40:42 +0000 (Fri, 07 Apr 2006) $
9# SVN revision $Rev: 330 $
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><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
56my %webvar = parse_post();
57cleanInput(\%webvar);
58
59print "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
65if(!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
85main 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>
91Allocate 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>
95Manually 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>
102Bump "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
108internal 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
112include 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.
120if ($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&nbsp;<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>
301Note: 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>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 "<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>\n";
312 }
313} elsif ($webvar{action} eq 'newcust') {
314 if ($webvar{custid} eq '') {
315 print 'No CustID entered. PTHBT! (Hit "Back" and fix the problem.)';
316 } else {
317 $sth = $ip_dbh->prepare("insert into customers ".
318 "(custid, name, street, city, province, country, pocode, ".
319 "phone, tech_handle, abuse_handle, admin_handle) values ".
320 "('$webvar{custid}', '$webvar{name}', '$webvar{street}', ".
321 "'$webvar{city}', '$webvar{province}', '$webvar{country}', ".
322 "'$webvar{pocode}', '$webvar{phone}', '$webvar{techhandle}', ".
323 "'$webvar{abusehandle}', '$webvar{adminhandle}')");
324 $sth->execute;
325 if ($sth->err) {
326 print "INSERT failed: ".$sth->errstr."\n";
327 } else {
328 print "Success! Added customer contact data:\n".
329 qq(<table border=1><tr>
330<td>CustID:</td>$webvar{custid}</td><td>Name:</td>$webvar{name}</td></tr>
331<tr><td>Street:</td><td>$webvar{street}</td></tr>
332<tr><td>City:</td><td>$webvar{city}</td><td>Province:</td><td>$webvar{province}</td></tr>
333<tr><td>Country:</td><td>$webvar{country}</td>
334<td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr>
335<tr><td>Phone:</td><td>$webvar{phone}</td>
336<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
337<tr><td>Description:</td><td><input name=description></td> -->
338<td>ARIN Handles:</td><td>
339 Tech: $webvar{tech_handle}<br>
340 Abuse: $webvar{abuse_handle}<br>
341 Admin: $webvar{admin_handle}<br>
342</td></tr></table>
343);
344 } # $sth err check
345 } # bad custid
346} elsif ($webvar{action} eq 'updcust') {
347 print "Updated $webvar{custid}\n";
348} elsif ($webvar{action} eq 'showpools') {
349 print "IP Pools currently allocated:\n".
350 "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
351 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
352 $sth->execute;
353 my %poolfree;
354 while (my @data = $sth->fetchrow_array) {
355 $poolfree{$data[0]} = 0;
356 }
357 $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
358 $sth->execute;
359 while (my @data = $sth->fetchrow_array) {
360 $poolfree{$data[0]}++;
361 }
362 foreach my $key (keys %poolfree) {
363 print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
364 "<td>$poolfree{$key}</td></tr>\n";
365 }
366 print "</table>\n";
367} elsif ($webvar{action} eq 'tweakpool') {
368 showPool($webvar{pool});
369} elsif ($webvar{action} eq 'updatepool') {
370
371 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
372 "city='$webvar{city}', type='$webvar{type}', available='".
373 (($webvar{available} eq 'y') ? 'y' : 'n').
374 "', notes='$webvar{notes}', description='$webvar{desc}' ".
375 "where ip='$webvar{ip}'");
376 $sth->execute;
377 if ($sth->err) {
378 print "Error updating pool IP $webvar{ip}: $@<hr>\n";
379 syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
380 } else {
381 $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
382 $sth->execute;
383 my @data = $sth->fetchrow_array;
384 print "$webvar{ip} in $data[0] updated\n<hr>\n";
385 syslog "notice", "$authuser updated pool IP $webvar{ip}";
386 }
387} elsif ($webvar{action} eq 'showusers') {
388 print "Notes:<br>\n".
389 "<li>Admin users automatically get all other priviledges.\n".
390 "<li>Everyone has basic read access.\n".
391 "<hr>Add new user:<form action=admin.cgi method=POST>\n".
392 "Username: <input name=username><br>\n".
393 "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
394 "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
395
396 print "<hr>Users with access:\n<table border=1>\n";
397 print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
398 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
399 "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
400 "<form action=admin.cgi method=POST>\n";
401 $sth = $ip_dbh->prepare("select username,acl from users order by username");
402 $sth->execute;
403 while (my @data = $sth->fetchrow_array) {
404 print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
405 qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
406 # Now for the fun bit. We have to pull apart the ACL field and
407 # output a bunch of checkboxes.
408 "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
409 "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
410 "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
411 "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
412 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
413 qq(></td><td><input type=submit value="Update"></td></form>\n).
414 "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
415 "<input type=hidden name=username value=$data[0]>".
416 qq(<input type=submit value="Delete user"></tr></form>\n);
417
418 }
419 print "</table>\n";
420} elsif ($webvar{action} eq 'updacl') {
421 print "Updating ACL for $webvar{username}:<br>\n";
422 my $acl = 'b';
423 if ($webvar{admin} eq 'on') {
424 $acl .= "acdsA";
425 } else {
426 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
427 ($webvar{change} eq 'on' ? 'c' : '').
428 ($webvar{del} eq 'on' ? 'd' : '').
429 ($webvar{sysnet} eq 'on' ? 's' : '');
430 }
431 print "New ACL: $acl<br>\n";
432
433 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
434 $sth->execute;
435 print "OK\n" if !$sth->err;
436
437 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
438
439} elsif ($webvar{action} eq 'newuser') {
440 print "Adding user $webvar{username}...\n";
441 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
442 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
443 $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
444 "('$webvar{username}','$cr_pass','b')");
445 $sth->execute;
446 if ($sth->err) {
447 print "<br>Error adding user: ".$sth->errstr;
448 } else {
449 print "OK\n";
450 }
451
452 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
453
454} elsif ($webvar{action} eq 'deluser') {
455 print "Deleting user $webvar{username}.<br>\n";
456 $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
457 $sth->execute;
458 print "OK\n" if !$sth->err;
459
460 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
461
462} elsif ($webvar{action} ne '<NULL>') {
463 print "webvar{action} check failed: Don't know how to $webvar{action}";
464}
465
466# Hokay. This is a little different. We have a few specific functions here:
467# -> Assign arbitrary subnet from arbitrary free space
468# -> Tweak individual DB fields
469#
470
471
472printFooter;
473
474$ip_dbh->disconnect;
475
476exit;
477
478
479# Tweak allocfrom into shape.
480sub fix_allocfrom {
481 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
482 # 3-octet class C specified
483 $webvar{allocfrom} .= ".0/24";
484 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
485 # 4-octet IP specified;
486 $webvar{allocfrom} .= "/24";
487 }
488}
489
490
491# List free blocks in a /24 for arbitrary manual allocation
492sub showfree($) {
493 my $cidr = new NetAddr::IP $_[0];
494 print "Showing free blocks in $cidr<br>\n".
495 "<table border=1>\n";
496 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
497 $sth->execute;
498 while (my @data = $sth->fetchrow_array) {
499 my $temp = new NetAddr::IP $data[0];
500 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
501 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
502 "<td>".
503 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
504 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
505 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
506 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
507 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
508 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
509 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
510 "</td>".
511 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
512 "\n</form></tr>\n";
513 }
514 print "</table>\n";
515}
516
517
518# Show allocations to allow editing.
519sub showAllocs($) {
520 my $cidr = new NetAddr::IP $_[0];
521 print "Edit custID, allocation type, city for allocations in ".
522 "$cidr:\n<table border=1>";
523 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
524 $sth->execute;
525 while (my @data = $sth->fetchrow_array) {
526 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
527 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
528 qq(<td><input name=custid value="$data[1]"></td>\n);
529
530 print "<td><select name=alloctype><option".
531 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
532 (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option".
533 (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option".
534 (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option".
535 (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
536 (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
537 (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option".
538 (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option".
539 (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
540 (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
541 (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
542 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
543 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
544 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
545 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
546 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option".
547 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
548 "</select></td>\n";
549 print qq(<td><input name=city value="$data[3]"></td>\n).
550 "<td>$data[4]</td><td>$data[5]</td>".
551 qq(<td><input type=submit value="Update"></td></form></tr>\n);
552 }
553 print "</table>\n";
554
555 # notes
556 print "<hr><b>Notes:</b>\n".
557 "<ul>\n<li>Use the main interface to update description and notes fields\n".
558 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
559 "</ul>\n";
560}
561
562
563# Stuff updates into DB
564sub update {
565 eval {
566 # Relatively simple SQL transaction here. Note that we're deliberately NOT
567 # updating notes/desc here as it's available through the main interface.
568 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
569 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
570 $sth->execute;
571 $ip_dbh->commit;
572 };
573 if ($@) {
574 carp "Transaction aborted because $@";
575 eval { $ip_dbh->rollback; };
576 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
577 } else {
578 # If we get here, the operation succeeded.
579 syslog "notice", "$authuser updated $webvar{block}";
580 print "Allocation $webvar{block} updated<hr>\n";
581 }
582 # need to get /24 that block is part of
583 my @bits = split /\./, $webvar{block};
584 $bits[3] = "0/24";
585 showAllocs((join ".", @bits));
586}
587
588
589# showPool()
590# List all IPs in a pool, and allow arbitrary admin changes to each
591# Allow changes to ALL fields
592sub showPool($) {
593 my $pool = new NetAddr::IP $_[0];
594 print qq(Listing pool $pool:\n<table border=1>
595<form action=admin.cgi method=POST>
596<input type=hidden name=action value=updatepool>
597<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
598<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
599<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>
600<option value="si">Static IP - Server pool</option>
601<option value="ci">Static IP - Cable</option>
602<option value="di">Static IP - DSL</option>
603<option value="mi">Static IP - Dialup</option>
604<option value="wi">Static IP - Wireless</option>
605</select></td></tr>
606<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
607<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
608<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
609<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
610).
611 "</table>Update the following record:<table border=1>\n";
612 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
613 $sth->execute;
614 while (my @data = $sth->fetchrow_array) {
615 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
616 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
617 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
618 }
619 print "</form></table>\n";
620}
Note: See TracBrowser for help on using the repository browser.