source: branches/htmlform/cgi-bin/admin.cgi@ 448

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

/branches/htmlform

Switch all scripts to use CGI::Simple for HTML form data munging
instead of legacy CommonWeb.pm sub. Remove parse_post() sub from
CommonWeb.pm. See #15.

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