source: branches/dns/cgi-bin/admin.cgi@ 533

Last change on this file since 533 was 262, checked in by Kris Deugau, 19 years ago

/branches/dns

Update branch base with trunk changes from r216:261

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