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

Last change on this file since 447 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
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-27 17:15:56 +0000 (Tue, 27 Jul 2010) $
9# SVN revision $Rev: 447 $
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 CGI::Simple;
18use DBI;
19use CommonWeb qw(:ALL);
20use CustIDCK;
21#use POSIX qw(ceil);
22use NetAddr::IP;
23
24use Sys::Syslog;
25
26# don't remove! required for GNU/FHS-ish install from tarball
27##uselib##
28
29use MyIPDB;
30
31openlog "IPDB-admin","pid","$IPDB::syslog_facility";
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
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
54if ($IPDBacl{$authuser} !~ /A/) {
55 print "Content-Type: text/html\n\n".
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".
60 qq(Access to this tool is restricted. Contact the <a href="mailto:ipdbadmin\@example.com">IPDB administrator</a> \n).
61 "for more information.\n</body>\n</html>\n";
62 exit;
63}
64
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;
69
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
74print "Content-type: text/html\n\n".
75 "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n".
76 qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
77 qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
78 "</head>\n<body>\n".
79 "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
80
81if(!defined($webvar{action})) {
82 $webvar{action} = "<NULL>"; #shuts up the warnings.
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
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
100 print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
101main interface if at all possible.
102<hr>
103<form action="admin.cgi" method="POST">
104<input type=hidden name=action value=alloc>
105Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
106<input type=submit value=" GIMME!! "></form>
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">
111</form>
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>
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.
120
121<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
122
123<hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users; change
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
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)
132);
133} else {
134 print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
135}
136
137
138## Possible actions.
139if ($webvar{action} eq 'alloc') {
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 '') {
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 ".
162 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
163 "non-customer assignments.");
164 return;
165 }
166 }
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];
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];
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">
236</form>
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 {
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}'";
263 mailNotify($ip_dbh, "a$webvar{alloctype}",
264 "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
265 " $webvar{custid}\n".
266 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
267 }
268 } else {
269 my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
270 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
271 $webvar{circid});
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
282
283 } # done city check
284
285} elsif ($webvar{action} eq 'alloctweak') {
286 fix_allocfrom();
287 showAllocs($webvar{allocfrom});
288} elsif ($webvar{action} eq 'update') {
289 update();
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;
307} elsif ($webvar{action} eq 'touch') {
308 print "Touching master $webvar{whichmaster}\n";
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 }
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>
318<input type=hidden name=action value=edcust>
319<input type=hidden name=newcust value=1>
320<td>CustID:</td><td><input name=custid></td>
321<td align=center><input type=submit value="Go to edit page for this custid"></td></tr>
322</form></table>
323);
324 print "<p>Click CustID to edit existing customer contact data:\n".
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) {
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";
331 }
332 print "</table>\n";
333} elsif ($webvar{action} eq 'edcust') {
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 }
339 $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
340 "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ".
341 "from customers where custid='$webvar{custid}'");
342 $sth->execute;
343 my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) =
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>
350<tr><td>Street:</td><td><input name=street value="$street"></td>
351<!-- <td>Street2:</td><td><input name=street2></td> -->
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>
357<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
358<td>Description:</td><td><input name=description></td> -->
359<tr><td>Contacts/ARIN Handles:</td><td>
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.
364</td>
365<td>"Special":</td><td><textarea name=special rows=4 cols=50>$special</textarea></td>
366</tr>
367<tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
368</form></table>
369<div style="margin-left:5px">
370<h3>Explanation for "Special" field:</h3>
371This is a temporary place to define the WHOIS "net name" for a block.
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:
376<pre>NetName192.168.236.0/24: MEGAWIDGET-1</pre>
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>
384);
385
386} elsif ($webvar{action} eq 'updcust') {
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>
410<tr><td>"Special":</td><td><pre>$webvar{special}</pre></td></tr>
411</table>
412<a href="admin.cgi?action=listcust">Back</a> to rWHOIS customer list<br>\n);
413
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";
417 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
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') {
436
437 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
438 "city='$webvar{city}', type='$webvar{type}', available='".
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 }
453} elsif ($webvar{action} eq 'showusers') {
454 print "Notes:<br>\n".
455 "<li>Admin users automatically get all other priviledges.\n".
456 "<li>Everyone has basic read access.\n".
457 "<hr>Add new user:<form action=admin.cgi method=POST>\n".
458 "Username: <input name=username><br>\n".
459 "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
460 "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
461
462 print "<hr>Users with access:\n<table border=1>\n";
463 print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
464 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
465 "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
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' : '').
477 "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
478 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
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);
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') {
490 $acl .= "acdsA";
491 } else {
492 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
493 ($webvar{change} eq 'on' ? 'c' : '').
494 ($webvar{del} eq 'on' ? 'd' : '').
495 ($webvar{sysnet} eq 'on' ? 's' : '');
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
503 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
504
505} elsif ($webvar{action} eq 'newuser') {
506 print "Adding user $webvar{username}...\n";
507 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
508 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
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
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 }
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);
547
548# add new entries from this tangle:
549 print "<h4>Add new notification:</h4>\n".
550 "Note: Failure notices on most conditions are not yet supported.\n";
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>
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>
566</td>
567<td>Failure?</td><td><input type=checkbox name=onfail></td></tr>
568<tr><td>Event/Allocation type:</td><td colspan=3>
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>
573 <td><input type=radio name=alloctype value=ci>New city</td>
574 <td><input type=radio name=alloctype value=no>New node</td>
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') {
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;
603 $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
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 (?,?)");
606##fixme: automagically merge reciplists iff action already exists
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 }
613 } else {
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};
616 }
617 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
618
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
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);
651} elsif ($webvar{action} ne '<NULL>') {
652 print "webvar{action} check failed: Don't know how to $webvar{action}";
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
660print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n);
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
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
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).
718 qq(<td><input name=custid value="$data[1]"></td>\n).
719 "<td><select name=alloctype>";
720
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
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; };
756 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
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}
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>
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>
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";
794 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
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}
803
804
805# interpret the notify codes
806sub dispNoticeCode {
807 my $code = shift;
808 my $action_out = '';
809
810 if ($code =~ /^s:/) {
811 $code =~ s/^s:/Special: /;
812 return $code;
813 }
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?
829# What about the eleventy-billion odd combinations possible?
830# this should give an idea of the structure tho
831 if ($target eq 'a') { $action_out .= "all"; }
832 elsif ($target eq '.i') {
833 $action_out .= "all static IPs";
834 }
835 else { $action_out .= $disp_alloctypes{$target}; }
836 }
837 return $action_out;
838}
Note: See TracBrowser for help on using the repository browser.