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

Last change on this file since 420 was 418, checked in by Kris Deugau, 14 years ago

/trunk

Shuffle pieces of admin.cgi dealing with rWHOIS customer data
to more closely match the PHP versions. This should reduce some
HTML duplication. (Preparation for #3)

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