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

Last change on this file since 257 was 256, checked in by Kris Deugau, 20 years ago

/trunk

Merge bugfixes from /branches/stable r248:252.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 18.8 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: 2005-05-31 18:24:33 +0000 (Tue, 31 May 2005) $
9# SVN revision $Rev: 256 $
10# Last update by $Author: kdeugau $
11###
12# Copyright (C) 2004,2005 - 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 print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
78main interface if at all possible.
79<hr>
80<a href="admin.cgi?action=newalloc">Add allocation</a>
81<hr>
82<form action="admin.cgi" method="POST">
83<input type=hidden name=action value=alloc>
84Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
85<input type=submit value=" GIMME!! "></form>
86<hr><form action="admin.cgi" method="POST">
87<input type=hidden name=action value=alloctweak>
88Manually update allocation data in this /24: <input name=allocfrom>
89<input type=submit value="Show allocations">
90</form>
91<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
92<hr><a href="admin.cgi?action=showACL">Change ACLs</a> (change internal access controls -
93note that this does NOT include IP-based limits)
94);
95} else {
96 print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
97}
98
99
100## Possible actions.
101if ($webvar{action} eq 'alloc') {
102 # OK, we know what we're allocating.
103
104 if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
105 printAndExit("Can't allocate something that's not a netblock/ip");
106 }
107
108 $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
109 $sth->execute;
110 my @data = $sth->fetchrow_array;
111 my $custid = $data[0];
112 if ($custid eq '') {
113 # Type that doesn't have a default custid
114 $custid = $webvar{custid};
115 }
116##fixme Check billing DB here
117
118 my $cidr = new NetAddr::IP $webvar{cidr};
119 my @data;
120 if ($webvar{alloctype} eq 'rm') {
121 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
122 $sth->execute;
123 @data = $sth->fetchrow_array;
124# User deserves errors if user can't be bothered to find the free block first.
125 printAndExit("Can't allocate from outside a free block!!\n")
126 if !$data[0];
127 } elsif ($webvar{alloctype} =~ /^(.)i$/) {
128 $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
129 $sth->execute;
130 @data = $sth->fetchrow_array;
131# User deserves errors if user can't be bothered to find the pool and a free IP first.
132 printAndExit("Can't allocate static IP from outside a pool!!\n")
133 if !$data[0];
134 } else {
135 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
136 $sth->execute;
137 @data = $sth->fetchrow_array;
138# User deserves errors if user can't be bothered to find the free block first.
139 printAndExit("Can't allocate from outside a routed block!!\n")
140 if !$data[0];
141 }
142
143 my $alloc_from = new NetAddr::IP $data[0];
144 $sth->finish;
145
146 my $cities = '';
147 foreach my $city (@citylist) {
148 $cities .= "<option>$city</option>\n";
149 }
150
151 print qq(<table class=regular>
152<form method=POST action=admin.cgi>
153<tr class=color1>
154<td>Allocating:</td>
155<td>$cidr<input type=hidden name=cidr value="$cidr"></td>
156</tr><tr class=color2>
157<td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
158<input type=hidden name=alloctype value="$webvar{alloctype}"></td>
159</tr><tr class=color1>
160<td>Allocated from:</td>
161<td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
162</tr><tr class="color2">
163<td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
164</tr><tr class=color1>
165<td>Customer location:</td><td>
166<select name="city"><option selected>-</option>
167$cities
168</select>
169&nbsp;<a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
170</td>
171</tr>
172<tr class="color2">
173<td>Circuit ID:</td><td><input name=circid size=40></td>
174</tr><tr class="color1">
175<td>Description/Name:</td><td><input name="desc" size=40></td>
176</tr><tr class="color2">
177<td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
178</tr><tr class="warning">
179<td colspan=2><center>WARNING: This will IMMEDIATELY assign this block!!</center></td>
180</tr><tr class="color2">
181<td class="center" colspan="2"><input type="submit" value=" Assign "></td>
182<input type="hidden" name="action" value="confirm">
183</tr>
184</table>
185);
186
187
188} elsif ($webvar{action} eq 'confirm') {
189
190 print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
191 "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
192 # Only need to check city here.
193 if ($webvar{city} eq '-') {
194 printError("Invalid customer location! Go back and select customer's location.");
195 } else {
196 my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
197 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
198 $webvar{circid});
199 if ($retcode eq 'OK') {
200 print "Allocation OK!\n";
201
202 if ($webvar{alloctype} =~ /^.i$/) {
203 # Notify tech@example.com
204 mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
205 "$disp_alloctypes{$webvar{alloctype}} $msg allocated to customer $webvar{custid}\n".
206 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
207 }
208 syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
209 "'$webvar{alloctype}'";
210 } else {
211 print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
212 syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
213 "'$webvar{alloctype}' failed: '$msg'";
214 }
215 } # done city check
216
217} elsif ($webvar{action} eq 'alloctweak') {
218 fix_allocfrom();
219 showAllocs($webvar{allocfrom});
220} elsif ($webvar{action} eq 'update') {
221 update();
222} elsif ($webvar{action} eq 'assign') {
223 # Display a list of possible blocks within the requested block.
224 open (HTML, "../admin_alloc.html")
225 or croak "Could not open admin_alloc.html :$!";
226 my $html = join('', <HTML>);
227 $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
228 $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
229
230 my $from = new NetAddr::IP $webvar{allocfrom};
231 my @blocklist = $from->split($webvar{masklen});
232 my $availblocks;
233 foreach (@blocklist) {
234 $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
235 }
236 $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
237
238 print $html;
239} elsif ($webvar{action} eq 'showpools') {
240 print "IP Pools currently allocated:\n".
241 "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
242 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
243 $sth->execute;
244 my %poolfree;
245 while (my @data = $sth->fetchrow_array) {
246 $poolfree{$data[0]} = 0;
247 }
248 $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
249 $sth->execute;
250 while (my @data = $sth->fetchrow_array) {
251 $poolfree{$data[0]}++;
252 }
253 foreach my $key (keys %poolfree) {
254 print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
255 "<td>$poolfree{$key}</td></tr>\n";
256 }
257 print "</table>\n";
258} elsif ($webvar{action} eq 'tweakpool') {
259 showPool($webvar{pool});
260} elsif ($webvar{action} eq 'updatepool') {
261
262 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
263 "city='$webvar{city}', type='$webvar{type}', available='".
264 (($webvar{available} eq 'y') ? 'y' : 'n').
265 "', notes='$webvar{notes}', description='$webvar{desc}' ".
266 "where ip='$webvar{ip}'");
267 $sth->execute;
268 if ($sth->err) {
269 print "Error updating pool IP $webvar{ip}: $@<hr>\n";
270 syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
271 } else {
272 $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
273 $sth->execute;
274 my @data = $sth->fetchrow_array;
275 print "$webvar{ip} in $data[0] updated\n<hr>\n";
276 syslog "notice", "$authuser updated pool IP $webvar{ip}";
277 }
278} elsif ($webvar{action} eq 'showACL') {
279 print "Notes:<br>\n".
280 "<li>Users must be added to .htpasswd from the shell, for the time being.\n".
281 "<li>New accounts will be added to the ACL here every time this page is loaded.\n".
282 "<li>Old accounts will NOT be automatically deleted; they must be removed via shell.\n".
283 "<li>Admin users automatically get all other priviledges.\n";
284# open .htpasswd, and snag the userlist.
285 $sth = $ip_dbh->prepare("select count (*) from users where username=?");
286 open HTPASS, "<../../.htpasswd" or carp "BOO! No .htpasswd file!";
287 while (<HTPASS>) {
288 my ($username,$encpwd) = split /:/;
289 $sth->execute($username);
290 my @data = $sth->fetchrow_array;
291 if ($data[0] eq '0') {
292 my $sth2 = $ip_dbh->prepare("insert into users (username,password) values ('$username','$encpwd')");
293 $sth2->execute;
294 print "$username added with read-only privs to ACL<br>\n";
295 }
296 }
297
298 print "<hr>Users with access:\n<table border=1>\n";
299 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
300 "<td>Delete</td><td>Admin user</td></tr>\n".
301 "<form action=admin.cgi method=POST>\n";
302 $sth = $ip_dbh->prepare("select username,acl from users order by username");
303 $sth->execute;
304 while (my @data = $sth->fetchrow_array) {
305 print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
306 qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
307 # Now for the fun bit. We have to pull apart the ACL field and
308 # output a bunch of checkboxes.
309 "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
310 "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
311 "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
312 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
313 qq(></td><td><input type=submit value="Update"></td></tr></form>\n);
314
315 }
316 print "</table>\n";
317} elsif ($webvar{action} eq 'updacl') {
318 print "Updating ACL for $webvar{username}:<br>\n";
319 my $acl = 'b';
320 if ($webvar{admin} eq 'on') {
321 $acl .= "acdA";
322 } else {
323 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
324 ($webvar{change} eq 'on' ? 'c' : '').
325 ($webvar{del} eq 'on' ? 'd' : '');
326 }
327 print "New ACL: $acl<br>\n";
328
329 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
330 $sth->execute;
331 print "OK\n" if !$sth->err;
332
333 print qq(<hr><a href="admin.cgi?action=showACL">Back</a> to ACL listing\n);
334
335} elsif ($webvar{action} ne '<NULL>') {
336 print "webvar{action} check failed: Don't know how to $webvar{action}";
337}
338
339# Hokay. This is a little different. We have a few specific functions here:
340# -> Assign arbitrary subnet from arbitrary free space
341# -> Tweak individual DB fields
342#
343
344
345printFooter;
346
347$ip_dbh->disconnect;
348
349exit;
350
351
352# Tweak allocfrom into shape.
353sub fix_allocfrom {
354 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
355 # 3-octet class C specified
356 $webvar{allocfrom} .= ".0/24";
357 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
358 # 4-octet IP specified;
359 $webvar{allocfrom} .= "/24";
360 }
361}
362
363
364# List free blocks in a /24 for arbitrary manual allocation
365sub showfree($) {
366 my $cidr = new NetAddr::IP $_[0];
367 print "Showing free blocks in $cidr<br>\n".
368 "<table border=1>\n";
369 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
370 $sth->execute;
371 while (my @data = $sth->fetchrow_array) {
372 my $temp = new NetAddr::IP $data[0];
373 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
374 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
375 "<td>".
376 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
377 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
378 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
379 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
380 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
381 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
382 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
383 "</td>".
384 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
385 "\n</form></tr>\n";
386 }
387 print "</table>\n";
388}
389
390
391# Show allocations to allow editing.
392sub showAllocs($) {
393 my $cidr = new NetAddr::IP $_[0];
394 print "Edit custID, allocation type, city for allocations in ".
395 "$cidr:\n<table border=1>";
396 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
397 $sth->execute;
398 while (my @data = $sth->fetchrow_array) {
399 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
400 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
401 qq(<td><input name=custid value="$data[1]"></td>\n);
402
403 print "<td><select name=alloctype><option".
404 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
405 (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option".
406 (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option".
407 (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option".
408 (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
409 (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
410 (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option".
411 (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option".
412 (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
413 (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
414 (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
415 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
416 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
417 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
418 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
419 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option".
420 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
421 "</select></td>\n";
422 print qq(<td><input name=city value="$data[3]"></td>\n).
423 "<td>$data[4]</td><td>$data[5]</td>".
424 qq(<td><input type=submit value="Update"></td></form></tr>\n);
425 }
426 print "</table>\n";
427
428 # notes
429 print "<hr><b>Notes:</b>\n".
430 "<ul>\n<li>Use the main interface to update description and notes fields\n".
431 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
432 "</ul>\n";
433}
434
435
436# Stuff updates into DB
437sub update {
438 eval {
439 # Relatively simple SQL transaction here. Note that we're deliberately NOT
440 # updating notes/desc here as it's available through the main interface.
441 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
442 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
443 $sth->execute;
444 $ip_dbh->commit;
445 };
446 if ($@) {
447 carp "Transaction aborted because $@";
448 eval { $ip_dbh->rollback; };
449 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
450 } else {
451 # If we get here, the operation succeeded.
452 syslog "notice", "$authuser updated $webvar{block}";
453 print "Allocation $webvar{block} updated<hr>\n";
454 }
455 # need to get /24 that block is part of
456 my @bits = split /\./, $webvar{block};
457 $bits[3] = "0/24";
458 showAllocs((join ".", @bits));
459}
460
461
462# showPool()
463# List all IPs in a pool, and allow arbitrary admin changes to each
464# Allow changes to ALL fields
465sub showPool($) {
466 my $pool = new NetAddr::IP $_[0];
467 print qq(Listing pool $pool:\n<table border=1>
468<form action=admin.cgi method=POST>
469<input type=hidden name=action value=updatepool>
470<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
471<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
472<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>
473<option value="si">Static IP - Server pool</option>
474<option value="ci">Static IP - Cable</option>
475<option value="di">Static IP - DSL</option>
476<option value="mi">Static IP - Dialup</option>
477<option value="wi">Static IP - Wireless</option>
478</select></td></tr>
479<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
480<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
481<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
482<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
483).
484 "</table>Update the following record:<table border=1>\n";
485 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
486 $sth->execute;
487 while (my @data = $sth->fetchrow_array) {
488 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
489 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
490 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
491 }
492 print "</form></table>\n";
493}
Note: See TracBrowser for help on using the repository browser.