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

Last change on this file since 439 was 414, checked in by Kris Deugau, 15 years ago

/branches/stable

Merge CSS shuffle from r413. Pieces of r412 were needed in
admin.cgi as well. See #13.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 25.7 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: 2010-05-13 21:03:21 +0000 (Thu, 13 May 2010) $
9# SVN revision $Rev: 414 $
10# Last update by $Author: kdeugau $
11###
12# Copyright (C) 2004-2006 - Kris Deugau
13
14use strict;
15use warnings;
16use CGI::Carp qw(fatalsToBrowser);
17use DBI;
18use CommonWeb qw(:ALL);
19use MyIPDB;
20use CustIDCK;
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
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
49if ($IPDBacl{$authuser} !~ /A/) {
50 print "Content-Type: text/html\n\n".
51 "<html>\n<head>\n\t<title>Access denied</title>\n".
52 qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
53 qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
54 "</head>\n<body>\n".
55 qq(Access to this tool is restricted. Contact <a href="mailto:kdeugau\@vianet.ca">Kris</a> \n).
56 "for more information.\n</body>\n</html>\n";
57 exit;
58}
59
60my %webvar = parse_post();
61cleanInput(\%webvar);
62
63print "Content-type: text/html\n\n".
64 "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n".
65 qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
66 qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
67 "</head>\n<body>\n".
68 "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
69
70if(!defined($webvar{action})) {
71 $webvar{action} = "<NULL>"; #shuts up the warnings.
72
73 my $typelist = '';
74 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
75 $sth->execute;
76 my @data = $sth->fetchrow_array;
77 $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
78 while (my @data = $sth->fetchrow_array) {
79 $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
80 }
81
82 my $masterlist = '';
83 $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr");
84 $sth->execute;
85 while (my @data = $sth->fetchrow_array) {
86 $masterlist .= "<option value='$data[0]'>$data[0] ($data[1])</option>\n";
87 }
88
89 print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
90main interface if at all possible.
91<hr>
92<a href="admin.cgi?action=newalloc">Add allocation</a>
93<hr>
94<form action="admin.cgi" method="POST">
95<input type=hidden name=action value=alloc>
96Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
97<input type=submit value=" GIMME!! "></form>
98<hr><form action="admin.cgi" method="POST">
99<input type=hidden name=action value=alloctweak>
100Manually update allocation data in this /24: <input name=allocfrom>
101<input type=submit value="Show allocations">
102</form>
103
104<hr>rWHOIS tools:
105<form action="admin.cgi" method="POST">
106<input type=hidden name=action value=touch>
107Bump "last updated" timestamp on this master: <select name=whichmaster>$masterlist</select>
108<input type=submit value="Update timestamp"> (Sets timestamp to "now")</form>
109<a href="admin.cgi?action=listcust">Edit customer data for rWHOIS</a>
110
111<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
112<hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users; change
113internal access controls - note that this does NOT include IP-based limits)
114<hr>Consistency check tools<br>
115<a href="consistency-check.pl">General</a>: Check general netblock consistency.<br>
116<a href="freespace.pl">Free space</a>: List total and aggregate free space. Does not
117include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8)
118<hr>(r)WHOIS<br>
119<a href="list-cust.php">List customer data for WHOIS</a> - data used for blocks with the SWIP box checkmarked.
120Links to edit/add data are on this page.
121);
122} else {
123 print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
124}
125
126
127## Possible actions.
128if ($webvar{action} eq 'alloc') {
129 # OK, we know what we're allocating.
130
131 if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
132 printAndExit("Can't allocate something that's not a netblock/ip");
133 }
134
135 $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
136 $sth->execute;
137 my @data = $sth->fetchrow_array;
138 my $custid = $data[0];
139 if ($custid eq '') {
140 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
141 # Force uppercase for now...
142 $webvar{custid} =~ tr/a-z/A-Z/;
143 # Crosscheck with billing.
144 my $status = CustIDCK->custid_exist($webvar{custid});
145 if ($CustIDCK::Error) {
146 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
147 return;
148 }
149 if (!$status) {
150 printError("Customer ID not valid. Make sure the Customer ID ".
151 "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
152 "non-customer assignments.");
153 return;
154 }
155 }
156 # Type that doesn't have a default custid
157 $custid = $webvar{custid};
158 }
159
160 my $cidr = new NetAddr::IP $webvar{cidr};
161 my @data;
162 if ($webvar{alloctype} eq 'rm') {
163 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
164 $sth->execute;
165 @data = $sth->fetchrow_array;
166# User deserves errors if user can't be bothered to find the free block first.
167 printAndExit("Can't allocate from outside a free block!!\n")
168 if !$data[0];
169 } elsif ($webvar{alloctype} =~ /^(.)i$/) {
170 $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
171 $sth->execute;
172 @data = $sth->fetchrow_array;
173# User deserves errors if user can't be bothered to find the pool and a free IP first.
174 printAndExit("Can't allocate static IP from outside a pool!!\n")
175 if !$data[0];
176 } else {
177 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
178 $sth->execute;
179 @data = $sth->fetchrow_array;
180# User deserves errors if user can't be bothered to find the free block first.
181 printAndExit("Can't allocate from outside a routed block!!\n")
182 if !$data[0];
183 }
184
185 my $alloc_from = new NetAddr::IP $data[0];
186 $sth->finish;
187
188 my $cities = '';
189 foreach my $city (@citylist) {
190 $cities .= "<option>$city</option>\n";
191 }
192
193 print qq(<table class=regular>
194<form method=POST action=admin.cgi>
195<tr class=color1>
196<td>Allocating:</td>
197<td>$cidr<input type=hidden name=cidr value="$cidr"></td>
198</tr><tr class=color2>
199<td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
200<input type=hidden name=alloctype value="$webvar{alloctype}"></td>
201</tr><tr class=color1>
202<td>Allocated from:</td>
203<td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
204</tr><tr class="color2">
205<td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
206</tr><tr class=color1>
207<td>Customer location:</td><td>
208<select name="city"><option selected>-</option>
209$cities
210</select>
211&nbsp;<a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
212</td>
213</tr>
214<tr class="color2">
215<td>Circuit ID:</td><td><input name=circid size=40></td>
216</tr><tr class="color1">
217<td>Description/Name:</td><td><input name="desc" size=40></td>
218</tr><tr class="color2">
219<td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
220</tr><tr class="warning">
221<td colspan=2><center>WARNING: This will IMMEDIATELY assign this block!!</center></td>
222</tr><tr class="color2">
223<td class="center" colspan="2"><input type="submit" value=" Assign "></td>
224<input type="hidden" name="action" value="confirm">
225</form>
226</tr>
227</table>
228);
229
230
231} elsif ($webvar{action} eq 'confirm') {
232
233 print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
234 "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
235 # Only need to check city here.
236 if ($webvar{city} eq '-') {
237 printError("Invalid customer location! Go back and select customer's location.");
238 } else {
239 if ($webvar{alloctype} =~ /^.i$/) {
240 $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ".
241 "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ".
242 "where ip='$webvar{cidr}'");
243 $sth->execute;
244 if ($sth->err) {
245 print "Allocation failed! DBI said:\n".$sth->errstr."\n";
246 syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
247 "'$webvar{alloctype}' failed: '".$sth->errstr."'";
248 } else {
249 print "Allocation OK!\n";
250 syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
251 "'$webvar{alloctype}'";
252 # Notify tech@example.com
253 mailNotify('tech@example.com',"$disp_alloctypes{$webvar{alloctype}} allocation",
254 "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
255 " $webvar{custid}\n".
256 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
257 }
258 } else {
259 my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
260 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
261 $webvar{circid});
262 if ($retcode eq 'OK') {
263 print "Allocation OK!\n";
264 syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
265 "'$webvar{alloctype}'";
266 } else {
267 print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
268 syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
269 "'$webvar{alloctype}' failed: '$msg'";
270 }
271 } # static IP vs netblock
272
273 } # done city check
274
275} elsif ($webvar{action} eq 'alloctweak') {
276 fix_allocfrom();
277 showAllocs($webvar{allocfrom});
278} elsif ($webvar{action} eq 'update') {
279 update();
280} elsif ($webvar{action} eq 'assign') {
281 # Display a list of possible blocks within the requested block.
282 open (HTML, "../admin_alloc.html")
283 or croak "Could not open admin_alloc.html :$!";
284 my $html = join('', <HTML>);
285 $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
286 $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
287
288 my $from = new NetAddr::IP $webvar{allocfrom};
289 my @blocklist = $from->split($webvar{masklen});
290 my $availblocks;
291 foreach (@blocklist) {
292 $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
293 }
294 $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
295
296 print $html;
297} elsif ($webvar{action} eq 'touch') {
298 print "Touching master $webvar{whichmaster}\n";
299 $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
300 $sth->execute;
301 if ($sth->err) {
302 print "<p>Error updating modified timestamp on master $webvar{whichmaster}: ".$sth->errstr."\n";
303 }
304} elsif ($webvar{action} eq 'listcust') {
305 print qq(Add new entry:\n
306<form action=admin.cgi method=POST>
307<table border=1><tr>
308<input type=hidden name=action value=newcust>
309<td>CustID:</td><td><input name=custid></td>
310<td>Name:</td><td><input name=name></td></tr>
311<tr><td>Street:</td><td><input name=street></td></tr>
312<!-- <td>Street2:</td><td><input name=street2></td> -->
313<tr><td>City:</td><td><input name=city></td>
314<td>Province: (2-letter code)</td><td><input name=province value=ON length=2 size=2></td></tr>
315<tr><td>Country: (2-letter code)</td><td><input name=country value=CA length=2 size=2></td>
316<td>Postal/ZIP Code:</td><td><input name=pocode></td></tr>
317<tr><td>Phone:</td><td><input name=phone></td>
318<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
319<td>Description:</td><td><input name=description></td> -->
320<td>ARIN Handles:</td><td>
321 Tech: <input name=tech_handle value="VH25-ORG-ARIN"><br>
322 Abuse: <input name=abuse_handle><br>
323 Admin: <input name=admin_handle><br>
324Note: Only tech is required at the moment.
325</td></tr>
326<tr><td colspan=4 align=center><input type=submit value="Add"></td></tr>
327</form></table>
328);
329 print "<p>Click CustID to edit existing customer contact data:\n".
330 "<table border=1>\n<tr><td>CustID</td><td>Name</td><td>Tech handle</td></tr>\n";
331 $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
332 $sth->execute;
333 while (my @data = $sth->fetchrow_array) {
334 print qq(<tr><td><a href="admin.cgi?action=edcust&custid=$data[0]">$data[0]</td>).
335 "<td>$data[1]</td><td>$data[2]</td></tr>\n";
336 }
337 print "</table>\n";
338} elsif ($webvar{action} eq 'newcust') {
339 if ($webvar{custid} eq '') {
340 print 'No CustID entered. PTHBT! (Hit "Back" and fix the problem.)';
341 } else {
342 $sth = $ip_dbh->prepare("insert into customers ".
343 "(custid, name, street, city, province, country, pocode, ".
344 "phone, tech_handle, abuse_handle, admin_handle) values ".
345 "('$webvar{custid}', '$webvar{name}', '$webvar{street}', ".
346 "'$webvar{city}', '$webvar{province}', '$webvar{country}', ".
347 "'$webvar{pocode}', '$webvar{phone}', '$webvar{techhandle}', ".
348 "'$webvar{abusehandle}', '$webvar{adminhandle}')");
349 $sth->execute;
350 if ($sth->err) {
351 print "INSERT failed: ".$sth->errstr."\n";
352 } else {
353 print "Success! Added customer contact data:\n".
354 qq(<table border=1><tr>
355<td>CustID:</td>$webvar{custid}</td><td>Name:</td>$webvar{name}</td></tr>
356<tr><td>Street:</td><td>$webvar{street}</td></tr>
357<tr><td>City:</td><td>$webvar{city}</td><td>Province:</td><td>$webvar{province}</td></tr>
358<tr><td>Country:</td><td>$webvar{country}</td>
359<td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr>
360<tr><td>Phone:</td><td>$webvar{phone}</td>
361<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
362<tr><td>Description:</td><td><input name=description></td> -->
363<td>ARIN Handles:</td><td>
364 Tech: $webvar{tech_handle}<br>
365 Abuse: $webvar{abuse_handle}<br>
366 Admin: $webvar{admin_handle}<br>
367</td></tr></table>
368);
369 } # $sth err check
370 } # bad custid
371} elsif ($webvar{action} eq 'edcust') {
372 $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
373 "country,pocode,phone,tech_handle,abuse_handle,admin_handle ".
374 "from customers where custid='$webvar{custid}'");
375 $sth->execute;
376 my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin) =
377 $sth->fetchrow_array;
378 print qq(<form action=admin.cgi method=POST>
379<table border=1><tr>
380<input type=hidden name=action value=updcust>
381<td>CustID:</td><td>$custid<input type=hidden name=custid value=$custid></td>
382<td>Name:</td><td><input name=name value="$name"></td></tr>
383<tr><td>Street:</td><td><input name=street value="$street"></td></tr>
384<!-- <td>Street2:</td><td><input name=street2></td> -->
385<tr><td>City:</td><td><input name=city value="$city"></td>
386<td>Province: (2-letter code)</td><td><input name=province value="$prov" length=2 size=2></td></tr>
387<tr><td>Country: (2-letter code)</td><td><input name=country value="$country" length=2 size=2></td>
388<td>Postal/ZIP Code:</td><td><input name=pocode value="$pocode"></td></tr>
389<tr><td>Phone:</td><td><input name=phone value="$pocode"></td>
390<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
391<td>Description:</td><td><input name=description></td> -->
392<td>ARIN Handles:</td><td>
393 Tech: <input name=tech_handle value="$tech"><br>
394 Abuse: <input name=abuse_handle value="$abuse"><br>
395 Admin: <input name=admin_handle value="$admin"><br>
396Note: Only tech is required at the moment.
397</td></tr>
398<tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
399</form></table>
400);
401
402} elsif ($webvar{action} eq 'updcust') {
403 print "Updated $webvar{custid}\n";
404} elsif ($webvar{action} eq 'showpools') {
405 print "IP Pools currently allocated:\n".
406 "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
407 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
408 $sth->execute;
409 my %poolfree;
410 while (my @data = $sth->fetchrow_array) {
411 $poolfree{$data[0]} = 0;
412 }
413 $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
414 $sth->execute;
415 while (my @data = $sth->fetchrow_array) {
416 $poolfree{$data[0]}++;
417 }
418 foreach my $key (keys %poolfree) {
419 print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
420 "<td>$poolfree{$key}</td></tr>\n";
421 }
422 print "</table>\n";
423} elsif ($webvar{action} eq 'tweakpool') {
424 showPool($webvar{pool});
425} elsif ($webvar{action} eq 'updatepool') {
426
427 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
428 "city='$webvar{city}', type='$webvar{type}', available='".
429 (($webvar{available} eq 'y') ? 'y' : 'n').
430 "', notes='$webvar{notes}', description='$webvar{desc}' ".
431 "where ip='$webvar{ip}'");
432 $sth->execute;
433 if ($sth->err) {
434 print "Error updating pool IP $webvar{ip}: $@<hr>\n";
435 syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
436 } else {
437 $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
438 $sth->execute;
439 my @data = $sth->fetchrow_array;
440 print "$webvar{ip} in $data[0] updated\n<hr>\n";
441 syslog "notice", "$authuser updated pool IP $webvar{ip}";
442 }
443} elsif ($webvar{action} eq 'showusers') {
444 print "Notes:<br>\n".
445 "<li>Admin users automatically get all other priviledges.\n".
446 "<li>Everyone has basic read access.\n".
447 "<hr>Add new user:<form action=admin.cgi method=POST>\n".
448 "Username: <input name=username><br>\n".
449 "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
450 "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
451
452 print "<hr>Users with access:\n<table border=1>\n";
453 print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
454 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
455 "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
456 "<form action=admin.cgi method=POST>\n";
457 $sth = $ip_dbh->prepare("select username,acl from users order by username");
458 $sth->execute;
459 while (my @data = $sth->fetchrow_array) {
460 print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
461 qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
462 # Now for the fun bit. We have to pull apart the ACL field and
463 # output a bunch of checkboxes.
464 "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
465 "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
466 "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
467 "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
468 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
469 qq(></td><td><input type=submit value="Update"></td></form>\n).
470 "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
471 "<input type=hidden name=username value=$data[0]>".
472 qq(<input type=submit value="Delete user"></tr></form>\n);
473
474 }
475 print "</table>\n";
476} elsif ($webvar{action} eq 'updacl') {
477 print "Updating ACL for $webvar{username}:<br>\n";
478 my $acl = 'b';
479 if ($webvar{admin} eq 'on') {
480 $acl .= "acdsA";
481 } else {
482 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
483 ($webvar{change} eq 'on' ? 'c' : '').
484 ($webvar{del} eq 'on' ? 'd' : '').
485 ($webvar{sysnet} eq 'on' ? 's' : '');
486 }
487 print "New ACL: $acl<br>\n";
488
489 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
490 $sth->execute;
491 print "OK\n" if !$sth->err;
492
493 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
494
495} elsif ($webvar{action} eq 'newuser') {
496 print "Adding user $webvar{username}...\n";
497 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
498 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
499 $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
500 "('$webvar{username}','$cr_pass','b')");
501 $sth->execute;
502 if ($sth->err) {
503 print "<br>Error adding user: ".$sth->errstr;
504 } else {
505 print "OK\n";
506 }
507
508 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
509
510} elsif ($webvar{action} eq 'deluser') {
511 print "Deleting user $webvar{username}.<br>\n";
512 $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
513 $sth->execute;
514 print "OK\n" if !$sth->err;
515
516 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
517
518} elsif ($webvar{action} ne '<NULL>') {
519 print "webvar{action} check failed: Don't know how to $webvar{action}";
520}
521
522# Hokay. This is a little different. We have a few specific functions here:
523# -> Assign arbitrary subnet from arbitrary free space
524# -> Tweak individual DB fields
525#
526
527print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n);
528
529printFooter;
530
531$ip_dbh->disconnect;
532
533exit;
534
535
536# Tweak allocfrom into shape.
537sub fix_allocfrom {
538 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
539 # 3-octet class C specified
540 $webvar{allocfrom} .= ".0/24";
541 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
542 # 4-octet IP specified;
543 $webvar{allocfrom} .= "/24";
544 }
545}
546
547
548# List free blocks in a /24 for arbitrary manual allocation
549sub showfree($) {
550 my $cidr = new NetAddr::IP $_[0];
551 print "Showing free blocks in $cidr<br>\n".
552 "<table border=1>\n";
553 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
554 $sth->execute;
555 while (my @data = $sth->fetchrow_array) {
556 my $temp = new NetAddr::IP $data[0];
557 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
558 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
559 "<td>".
560 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
561 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
562 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
563 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
564 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
565 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
566 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
567 "</td>".
568 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
569 "\n</form></tr>\n";
570 }
571 print "</table>\n";
572}
573
574
575# Show allocations to allow editing.
576sub showAllocs($) {
577 my $cidr = new NetAddr::IP $_[0];
578 print "Edit custID, allocation type, city for allocations in ".
579 "$cidr:\n<table border=1>";
580 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
581 $sth->execute;
582 while (my @data = $sth->fetchrow_array) {
583 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
584 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
585 qq(<td><input name=custid value="$data[1]"></td>\n).
586 "<td><select name=alloctype>";
587
588 my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
589 " where listorder < 500 and not (type like '_i') order by listorder");
590 $sth2->execute;
591 while (my @types = $sth2->fetchrow_array) {
592 print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
593 " value='$types[0]'>$types[1]</option>\n";
594 }
595
596 print qq(<td><input name=city value="$data[3]"></td>\n).
597 "<td>$data[4]</td><td>$data[5]</td>".
598 qq(<td><input type=submit value="Update"></td></form></tr>\n);
599 }
600 print "</table>\n";
601
602 # notes
603 print "<hr><b>Notes:</b>\n".
604 "<ul>\n<li>Use the main interface to update description and notes fields\n".
605 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
606 "</ul>\n";
607}
608
609
610# Stuff updates into DB
611sub update {
612 eval {
613 # Relatively simple SQL transaction here. Note that we're deliberately NOT
614 # updating notes/desc here as it's available through the main interface.
615 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
616 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
617 $sth->execute;
618 $ip_dbh->commit;
619 };
620 if ($@) {
621 carp "Transaction aborted because $@";
622 eval { $ip_dbh->rollback; };
623 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
624 } else {
625 # If we get here, the operation succeeded.
626 syslog "notice", "$authuser updated $webvar{block}";
627 print "Allocation $webvar{block} updated<hr>\n";
628 }
629 # need to get /24 that block is part of
630 my @bits = split /\./, $webvar{block};
631 $bits[3] = "0/24";
632 showAllocs((join ".", @bits));
633}
634
635
636# showPool()
637# List all IPs in a pool, and allow arbitrary admin changes to each
638# Allow changes to ALL fields
639sub showPool($) {
640 my $pool = new NetAddr::IP $_[0];
641 print qq(Listing pool $pool:\n<table border=1>
642<form action=admin.cgi method=POST>
643<input type=hidden name=action value=updatepool>
644<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
645<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
646<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
647
648 $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
649 $sth->execute;
650 while (my @data = $sth->fetchrow_array) {
651 print "<option value='$data[0]'>$data[1]</option>\n";
652 }
653
654 print qq(</select></td></tr>
655<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
656<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
657<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
658<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
659).
660 "</table>Update the following record:<table border=1>\n";
661 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
662 $sth->execute;
663 while (my @data = $sth->fetchrow_array) {
664 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
665 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
666 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
667 }
668 print "</form></table>\n";
669}
Note: See TracBrowser for help on using the repository browser.