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

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

/trunk

Merge /branches/acl r221, 222, 223, 225, 226 back to trunk

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