source: branches/acl/cgi-bin/admin.cgi@ 394

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

/branches/acl

Bugfix and cosmetic cleanup. Remove the TEST warning in the heading,
fix the check for a "null" $webvar{action} so it doesn't trigger on
loading the "index".

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 18.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: 2005-04-19 18:18:08 +0000 (Tue, 19 Apr 2005) $
9# SVN revision $Rev: 241 $
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>[IPDB admin tools]</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 } 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
210} elsif ($webvar{action} eq 'alloctweak') {
211 fix_allocfrom();
212 showAllocs($webvar{allocfrom});
213} elsif ($webvar{action} eq 'update') {
214 update();
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;
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";
235 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
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') {
254
255 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
256 "city='$webvar{city}', type='$webvar{type}', available='".
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 }
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 chomp;
282 my ($username,$encpwd) = split /:/;
283 $sth->execute($username);
284 my @data = $sth->fetchrow_array;
285 if ($data[0] eq '0') {
286 my $sth2 = $ip_dbh->prepare("insert into users (username,password) values ('$username','$encpwd')");
287 $sth2->execute;
288 print "$username added with read-only privs to ACL<br>\n";
289 }
290 }
291
292 print "<hr>Users with access:\n<table border=1>\n";
293 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
294 "<td>Delete</td><td>Admin user</td></tr>\n".
295 "<form action=admin.cgi method=POST>\n";
296 $sth = $ip_dbh->prepare("select username,acl from users order by username");
297 $sth->execute;
298 while (my @data = $sth->fetchrow_array) {
299 print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
300 qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
301 # Now for the fun bit. We have to pull apart the ACL field and
302 # output a bunch of checkboxes.
303 "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
304 "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
305 "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
306 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
307 qq(></td><td><input type=submit value="Update"></td></tr></form>\n);
308
309 }
310 print "</table>\n";
311} elsif ($webvar{action} eq 'updacl') {
312 print "Updating ACL for $webvar{username}:<br>\n";
313 my $acl = 'b';
314 if ($webvar{admin} eq 'on') {
315 $acl .= "acdA";
316 } else {
317 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
318 ($webvar{change} eq 'on' ? 'c' : '').
319 ($webvar{del} eq 'on' ? 'd' : '');
320 }
321 print "New ACL: $acl<br>\n";
322
323 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
324 $sth->execute;
325 print "OK\n" if !$sth->err;
326
327 print qq(<hr><a href="admin.cgi?action=showACL">Back</a> to ACL listing\n);
328
329} elsif ($webvar{action} ne '<NULL>') {
330 print "webvar{action} check failed: Don't know how to $webvar{action}";
331}
332
333# Hokay. This is a little different. We have a few specific functions here:
334# -> Assign arbitrary subnet from arbitrary free space
335# -> Tweak individual DB fields
336#
337
338
339printFooter;
340
341$ip_dbh->disconnect;
342
343exit;
344
345
346# Tweak allocfrom into shape.
347sub fix_allocfrom {
348 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
349 # 3-octet class C specified
350 $webvar{allocfrom} .= ".0/24";
351 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
352 # 4-octet IP specified;
353 $webvar{allocfrom} .= "/24";
354 }
355}
356
357
358# List free blocks in a /24 for arbitrary manual allocation
359sub showfree($) {
360 my $cidr = new NetAddr::IP $_[0];
361 print "Showing free blocks in $cidr<br>\n".
362 "<table border=1>\n";
363 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
364 $sth->execute;
365 while (my @data = $sth->fetchrow_array) {
366 my $temp = new NetAddr::IP $data[0];
367 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
368 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
369 "<td>".
370 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
371 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
372 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
373 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
374 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
375 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
376 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
377 "</td>".
378 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
379 "\n</form></tr>\n";
380 }
381 print "</table>\n";
382}
383
384
385# Show allocations to allow editing.
386sub showAllocs($) {
387 my $cidr = new NetAddr::IP $_[0];
388 print "Edit custID, allocation type, city for allocations in ".
389 "$cidr:\n<table border=1>";
390 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
391 $sth->execute;
392 while (my @data = $sth->fetchrow_array) {
393 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
394 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
395 qq(<td><input name=custid value="$data[1]"></td>\n);
396
397 print "<td><select name=alloctype><option".
398 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
399 (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option".
400 (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option".
401 (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option".
402 (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
403 (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
404 (($data[2] eq 'sd') ? ' selected' : '') ." value='sd'>Static Pool - Server pool</option>\n<option".
405 (($data[2] eq 'cd') ? ' selected' : '') ." value='cd'>Static Pool - Cable</option>\n<option".
406 (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
407 (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
408 (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
409 (($data[2] eq 'en') ? ' selected' : '') ." value='en'>End-use netblock</option>\n<option".
410 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
411 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
412 (($data[2] eq 'ce') ? ' selected' : '') ." value='ce'>Dynamic cable netblock</option>\n<option".
413 (($data[2] eq 'we') ? ' selected' : '') ." value='we'>Dynamic WiFi netblock</option>\n<option".
414 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
415 "</select></td>\n";
416 print qq(<td><input name=city value="$data[3]"></td>\n).
417 "<td>$data[4]</td><td>$data[5]</td>".
418 qq(<td><input type=submit value="Update"></td></form></tr>\n);
419 }
420 print "</table>\n";
421
422 # notes
423 print "<hr><b>Notes:</b>\n".
424 "<ul>\n<li>Use the main interface to update description and notes fields\n".
425 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
426 "</ul>\n";
427}
428
429
430# Stuff updates into DB
431sub update {
432 eval {
433 # Relatively simple SQL transaction here. Note that we're deliberately NOT
434 # updating notes/desc here as it's available through the main interface.
435 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
436 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
437 $sth->execute;
438 $ip_dbh->commit;
439 };
440 if ($@) {
441 carp "Transaction aborted because $@";
442 eval { $ip_dbh->rollback; };
443 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
444 } else {
445 # If we get here, the operation succeeded.
446 syslog "notice", "$authuser updated $webvar{block}";
447 print "Allocation $webvar{block} updated<hr>\n";
448 }
449 # need to get /24 that block is part of
450 my @bits = split /\./, $webvar{block};
451 $bits[3] = "0/24";
452 showAllocs((join ".", @bits));
453}
454
455
456# showPool()
457# List all IPs in a pool, and allow arbitrary admin changes to each
458# Allow changes to ALL fields
459sub showPool($) {
460 my $pool = new NetAddr::IP $_[0];
461 print qq(Listing pool $pool:\n<table border=1>
462<form action=admin.cgi method=POST>
463<input type=hidden name=action value=updatepool>
464<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
465<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
466<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>
467<option value="si">Static IP - Server pool</option>
468<option value="ci">Static IP - Cable</option>
469<option value="di">Static IP - DSL</option>
470<option value="mi">Static IP - Dialup</option>
471<option value="wi">Static IP - Wireless</option>
472</select></td></tr>
473<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
474<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
475<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
476<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
477).
478 "</table>Update the following record:<table border=1>\n";
479 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
480 $sth->execute;
481 while (my @data = $sth->fetchrow_array) {
482 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
483 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
484 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
485 }
486 print "</form></table>\n";
487}
Note: See TracBrowser for help on using the repository browser.