source: branches/stable/cgi-bin/admin.cgi@ 250

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

/branches/stable

Fix error logging bug that dropped the alloctype code in admin.cgi

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