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

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

/trunk

Fix up add-notice process to allow adding new-city and new-node failures.
(Re-) closes #21.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 31.9 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-20 16:05:09 +0000 (Tue, 20 Jul 2010) $
9# SVN revision $Rev: 438 $
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","$IPDB::syslog_facility";
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<form action="admin.cgi" method="POST">
97<input type=hidden name=action value=alloc>
98Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
99<input type=submit value=" GIMME!! "></form>
100<hr><form action="admin.cgi" method="POST">
101<input type=hidden name=action value=alloctweak>
102Manually update allocation data in this /24: <input name=allocfrom>
103<input type=submit value="Show allocations">
104</form>
105
106<hr>rWHOIS tools:
107<form action="admin.cgi" method="POST">
108<input type=hidden name=action value=touch>
109Bump "last updated" timestamp on this master: <select name=whichmaster>$masterlist</select>
110<input type=submit value="Update timestamp"> (Sets timestamp to "now")</form>
111<a href="admin.cgi?action=listcust">Edit customer data for rWHOIS</a> - data used for
112blocks with the SWIP box checkmarked. Links to edit/add data are on this page.
113
114<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
115
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)<br>
118<a href="admin.cgi?action=emailnotice">Manage email notice options</a> (pick which events
119and allocation types cause notifications; configure recipient lists for notices)
120
121<hr>Consistency check tools<br>
122<a href="consistency-check.pl">General</a>: Check general netblock consistency.<br>
123<a href="freespace.pl">Free space</a>: List total and aggregate free space. Does not
124include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8)
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, "a$webvar{alloctype}",
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 align=center><input type=submit value="Go to edit page for this custid"></td></tr>
315</form></table>
316);
317 print "<p>Click CustID to edit existing customer contact data:\n".
318 "<table border=1>\n<tr><td>CustID</td><td>Name</td><td>Tech handle</td></tr>\n";
319 $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
320 $sth->execute;
321 while (my @data = $sth->fetchrow_array) {
322 print qq(<tr><td><a href="admin.cgi?action=edcust&custid=$data[0]">$data[0]</td>).
323 "<td>$data[1]</td><td>$data[2]</td></tr>\n";
324 }
325 print "</table>\n";
326} elsif ($webvar{action} eq 'edcust') {
327 if ($webvar{newcust}) {
328 print "got here?\n";
329 $sth = $ip_dbh->prepare("INSERT INTO customers (custid) VALUES (?)");
330 $sth->execute($webvar{custid});
331 }
332 $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
333 "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ".
334 "from customers where custid='$webvar{custid}'");
335 $sth->execute;
336 my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) =
337 $sth->fetchrow_array;
338 print qq(<form action=admin.cgi method=POST>
339<table border=1><tr>
340<input type=hidden name=action value=updcust>
341<td>CustID:</td><td>$custid<input type=hidden name=custid value=$custid></td>
342<td>Name:</td><td><input name=name value="$name"></td></tr>
343<tr><td>Street:</td><td><input name=street value="$street"></td>
344<!-- <td>Street2:</td><td><input name=street2></td> -->
345<td>City:</td><td><input name=city value="$city"></td></tr>
346<tr><td>Province/State: (2-letter code)</td><td><input name=province value="$prov" length=2 size=2></td>
347<td>Country: (2-letter code)</td><td><input name=country value="$country" length=2 size=2></td></tr>
348<tr><td>Postal/ZIP Code:</td><td><input name=pocode value="$pocode"></td>
349<td>Phone:</td><td><input name=phone value="$pocode"></td></tr>
350<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
351<td>Description:</td><td><input name=description></td> -->
352<tr><td>Contacts/ARIN Handles:</td><td>
353 Tech: <input name=tech_handle value="$tech"><br>
354 Abuse: <input name=abuse_handle value="$abuse"><br>
355 Admin: <input name=admin_handle value="$admin"><br>
356Note: Only tech is required at the moment.
357</td>
358<td>"Special":</td><td><textarea name=special rows=4 cols=50>$special</textarea></td>
359</tr>
360<tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
361</form></table>
362<div style="margin-left:5px">
363<h3>Explanation for "Special" field:</h3>
364This is a temporary place to define the WHOIS "net name" for a block.
365It may be removed later, more likely migrated elsewhere.
366<p>It's formatted like this, one line for each custom net name:
367<pre>NetName[CIDR block]: NET-NAME</pre>
368Example:
369<pre>NetName192.168.236.0/24: MEGAWIDGET-1</pre>
370Note:
371<ul style="margin-top: 0px;">
372<li>Spacing is important - there should only be ONE space, in between the colon and the net name.
373<li>The CIDR block name nust include all four octets - no short forms are accepted.
374<li>Net names must be all uppercase, and consist only of A-Z, 0-9, and - (same as for SWIPed net names).
375</ul>
376</div>
377);
378
379} elsif ($webvar{action} eq 'updcust') {
380 $sth = $ip_dbh->prepare("UPDATE customers SET".
381 " name=?, street=?, city=?, province=?, country=?, pocode=?,".
382 " phone=?, tech_handle=?, abuse_handle=?, admin_handle=?, special=?".
383 " WHERE custid=?");
384 $sth->execute($webvar{name}, $webvar{street}, $webvar{city}, $webvar{province},
385 $webvar{country}, $webvar{pocode}, $webvar{phone}, $webvar{tech_handle},
386 $webvar{abuse_handle}, $webvar{admin_handle}, $webvar{special}, $webvar{custid});
387 print "Updated $webvar{custid}<br>\n".
388 qq(<table border=1>
389<tr><td>CustID:</td><td>$webvar{custid}</td></tr>
390<tr><td>Name:</td><td>$webvar{name}</td></tr>
391<tr><td>Street:</td><td>$webvar{street}</td></tr>
392<tr><td>City:</td><td>$webvar{city}</td></tr>
393<tr><td>Province/State:</td><td>$webvar{province}</td></tr>
394<tr><td>Country:</td><td>$webvar{country}</td></tr>
395<tr><td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr>
396<tr><td>Phone:</td><td>$webvar{phone}</td></tr>
397<!-- <td>Default rDNS:</td><td>$webvar{def_rdns}</td></tr> -->
398<tr><td>Contacts/ARIN Handles:</td><td>
399 Tech: $webvar{tech_handle}<br>
400 Abuse: $webvar{abuse_handle}<br>
401 Admin: $webvar{admin_handle}<br>
402</td></tr>
403<tr><td>"Special":</td><td><pre>$webvar{special}</pre></td></tr>
404</table>
405<a href="admin.cgi?action=listcust">Back</a> to rWHOIS customer list<br>\n);
406
407} elsif ($webvar{action} eq 'showpools') {
408 print "IP Pools currently allocated:\n".
409 "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
410 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
411 $sth->execute;
412 my %poolfree;
413 while (my @data = $sth->fetchrow_array) {
414 $poolfree{$data[0]} = 0;
415 }
416 $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
417 $sth->execute;
418 while (my @data = $sth->fetchrow_array) {
419 $poolfree{$data[0]}++;
420 }
421 foreach my $key (keys %poolfree) {
422 print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
423 "<td>$poolfree{$key}</td></tr>\n";
424 }
425 print "</table>\n";
426} elsif ($webvar{action} eq 'tweakpool') {
427 showPool($webvar{pool});
428} elsif ($webvar{action} eq 'updatepool') {
429
430 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
431 "city='$webvar{city}', type='$webvar{type}', available='".
432 (($webvar{available} eq 'y') ? 'y' : 'n').
433 "', notes='$webvar{notes}', description='$webvar{desc}' ".
434 "where ip='$webvar{ip}'");
435 $sth->execute;
436 if ($sth->err) {
437 print "Error updating pool IP $webvar{ip}: $@<hr>\n";
438 syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
439 } else {
440 $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
441 $sth->execute;
442 my @data = $sth->fetchrow_array;
443 print "$webvar{ip} in $data[0] updated\n<hr>\n";
444 syslog "notice", "$authuser updated pool IP $webvar{ip}";
445 }
446} elsif ($webvar{action} eq 'showusers') {
447 print "Notes:<br>\n".
448 "<li>Admin users automatically get all other priviledges.\n".
449 "<li>Everyone has basic read access.\n".
450 "<hr>Add new user:<form action=admin.cgi method=POST>\n".
451 "Username: <input name=username><br>\n".
452 "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
453 "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
454
455 print "<hr>Users with access:\n<table border=1>\n";
456 print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
457 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
458 "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
459 "<form action=admin.cgi method=POST>\n";
460 $sth = $ip_dbh->prepare("select username,acl from users order by username");
461 $sth->execute;
462 while (my @data = $sth->fetchrow_array) {
463 print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
464 qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
465 # Now for the fun bit. We have to pull apart the ACL field and
466 # output a bunch of checkboxes.
467 "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
468 "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
469 "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
470 "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
471 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
472 qq(></td><td><input type=submit value="Update"></td></form>\n).
473 "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
474 "<input type=hidden name=username value=$data[0]>".
475 qq(<input type=submit value="Delete user"></tr></form>\n);
476
477 }
478 print "</table>\n";
479} elsif ($webvar{action} eq 'updacl') {
480 print "Updating ACL for $webvar{username}:<br>\n";
481 my $acl = 'b';
482 if ($webvar{admin} eq 'on') {
483 $acl .= "acdsA";
484 } else {
485 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
486 ($webvar{change} eq 'on' ? 'c' : '').
487 ($webvar{del} eq 'on' ? 'd' : '').
488 ($webvar{sysnet} eq 'on' ? 's' : '');
489 }
490 print "New ACL: $acl<br>\n";
491
492 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
493 $sth->execute;
494 print "OK\n" if !$sth->err;
495
496 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
497
498} elsif ($webvar{action} eq 'newuser') {
499 print "Adding user $webvar{username}...\n";
500 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
501 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
502 $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
503 "('$webvar{username}','$cr_pass','b')");
504 $sth->execute;
505 if ($sth->err) {
506 print "<br>Error adding user: ".$sth->errstr;
507 } else {
508 print "OK\n";
509 }
510
511 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
512
513} elsif ($webvar{action} eq 'deluser') {
514 print "Deleting user $webvar{username}.<br>\n";
515 $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
516 $sth->execute;
517 print "OK\n" if !$sth->err;
518
519 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
520
521} elsif ($webvar{action} eq 'emailnotice') {
522 print "<h4>Email notice management:</h4>\nClick the email addresses to edit that list.";
523 $sth = $ip_dbh->prepare("SELECT action,reciplist FROM notify");
524 $sth->execute;
525
526 print "<table border=1>\n";
527 while (my ($notice_code,$reciplist) = $sth->fetchrow_array() ) {
528##fixme: hairy mess, only a few things call mailNotify() anyway, so many possible notices won't work.
529 my $action_out = dispNoticeCode($notice_code);
530 print "<tr><td>$action_out</td>".
531 qq(<td><a href="admin.cgi?action=ednotice&code=$notice_code">$reciplist</a></td>).
532 qq(<td><a href="admin.cgi?action=delnotice&code=$notice_code">Delete</a></tr>\n);
533 }
534 print qq(<tr><td colspan=2>Known "special" codes:<br>
535<ul style="margin-top: 0px; margin-bottom: 0px;">
536 <li>swi: Notify if block being updated has SWIP flag set</li>
537</ul></td></tr>
538</table>
539);
540
541# add new entries from this tangle:
542 print "<h4>Add new notification:</h4>\n".
543 "Note: Failure notices on most conditions are not yet supported.\n";
544
545 print qq(<table border=1><form action=admin.cgi method="POST">
546<input type=hidden name=action value=addnotice>
547<tr>
548<td>Recipients</td><td colspan=3><textarea name=reciplist cols=50 rows=5></textarea></td></tr>
549<tr><td>Action</td><td>
550 <table><tr>
551 <td><input type=radio name=msgaction value=a>Add &nbsp;
552 <input type=radio name=msgaction value=u>Update &nbsp;
553 <input type=radio name=msgaction value=d>Delete &nbsp;
554 <input type=radio name=msgaction value=n>New listitem</td>
555 </tr><tr>
556 <td>
557 <input type=radio name=msgaction value=s:>Special: <input name=special>(requires code changes)
558 </td></tr></table>
559</td>
560<td>Failure?</td><td><input type=checkbox name=onfail></td></tr>
561<tr><td>Event/Allocation type:</td><td colspan=3>
562 <table>
563 <tr>
564 <td><input type=radio name=alloctype value=a>All allocations</td>
565 <td><input type=radio name=alloctype value=.i>All static IPs</td>
566 <td><input type=radio name=alloctype value=ci>New city</td>
567 <td><input type=radio name=alloctype value=no>New node</td>
568 </tr>
569 <tr>
570);
571
572 $sth = $ip_dbh->prepare("SELECT type,dispname FROM alloctypes WHERE listorder < 500 ".
573 "ORDER BY listorder");
574 $sth->execute;
575 my $i=0;
576 while (my ($type,$disp) = $sth->fetchrow_array) {
577 print " <td><input type=radio name=alloctype value=$type>$disp</td>";
578 $i++;
579 print " </tr>\n\t<tr>"
580 if ($i % 4 == 0);
581 }
582
583 print qq( </tr>
584 </table>
585</tr>
586<tr><td colspan=4 align=center><input type=submit value="Add notice"></td></tr>
587</table>
588</form>
589);
590 ## done spitting out add-new-spam-me-now table
591
592} elsif ($webvar{action} eq 'addnotice') {
593 $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:';
594 if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) {
595 $webvar{reciplist} =~ s/[\r\n]+/,/g;
596 $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
597 print "Adding notice to $webvar{reciplist} for ".dispNoticeCode($webvar{msgaction}.$webvar{alloctype}).":\n";
598 $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)");
599##fixme: automagically merge reciplists iff action already exists
600 $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist});
601 if ($sth->err) {
602 print "Failed: DB error: ".$sth->errstr."\n";
603 } else {
604 print "OK!<br>\n"
605 }
606 } else {
607 print "Need to specify at least one recipient, an action, and an allocation type. ".
608 qq{("Special" content is considered an allocation type). Hit the Back button and try again.<br>\n};
609 }
610 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
611
612} elsif ($webvar{action} eq 'delnotice') {
613 print "Deleting notices on ".dispNoticeCode($webvar{code}.$webvar{alloctype}).":\n";
614 $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?");
615 $sth->execute($webvar{code});
616 if ($sth->err) {
617 print "Failed: DB error: ".$sth->errstr."\n";
618 } else {
619 print "OK!<br>\n"
620 }
621 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
622
623} elsif ($webvar{action} eq 'ednotice') {
624 print "<h4>Editing recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
625 $sth = $ip_dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
626 $sth->execute($webvar{code});
627 my ($reciplist) = $sth->fetchrow_array;
628 $reciplist =~ s/,/\n/g;
629 print qq(<form action=admin.cgi method=POST><input type=hidden name=code value="$webvar{code}">\n).
630 qq(<input type=hidden name=action value="updnotice"><table border=1><tr><td>).
631 qq(<textarea cols="40" rows="5" name=reciplist>$reciplist</textarea></td><td><input type=submit value="Update">\n).
632 "</td></tr></table></form>\n";
633} elsif ($webvar{action} eq 'updnotice') {
634 print "<h4>Updating recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
635 $sth = $ip_dbh->prepare("UPDATE notify SET reciplist=? WHERE action=?");
636 $webvar{reciplist} =~ s/[\r\n]+/,/g;
637 $sth->execute($webvar{reciplist}, $webvar{code});
638 if ($sth->err) {
639 print "Failed: DB error: ".$sth->errstr."\n";
640 } else {
641 print "OK!<br>\n"
642 }
643 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
644} elsif ($webvar{action} ne '<NULL>') {
645 print "webvar{action} check failed: Don't know how to $webvar{action}";
646}
647
648# Hokay. This is a little different. We have a few specific functions here:
649# -> Assign arbitrary subnet from arbitrary free space
650# -> Tweak individual DB fields
651#
652
653print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n);
654
655printFooter;
656
657$ip_dbh->disconnect;
658
659exit;
660
661
662# Tweak allocfrom into shape.
663sub fix_allocfrom {
664 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
665 # 3-octet class C specified
666 $webvar{allocfrom} .= ".0/24";
667 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
668 # 4-octet IP specified;
669 $webvar{allocfrom} .= "/24";
670 }
671}
672
673
674# List free blocks in a /24 for arbitrary manual allocation
675sub showfree($) {
676 my $cidr = new NetAddr::IP $_[0];
677 print "Showing free blocks in $cidr<br>\n".
678 "<table border=1>\n";
679 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
680 $sth->execute;
681 while (my @data = $sth->fetchrow_array) {
682 my $temp = new NetAddr::IP $data[0];
683 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
684 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
685 "<td>".
686 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
687 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
688 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
689 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
690 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
691 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
692 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
693 "</td>".
694 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
695 "\n</form></tr>\n";
696 }
697 print "</table>\n";
698}
699
700
701# Show allocations to allow editing.
702sub showAllocs($) {
703 my $cidr = new NetAddr::IP $_[0];
704 print "Edit custID, allocation type, city for allocations in ".
705 "$cidr:\n<table border=1>";
706 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
707 $sth->execute;
708 while (my @data = $sth->fetchrow_array) {
709 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
710 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
711 qq(<td><input name=custid value="$data[1]"></td>\n).
712 "<td><select name=alloctype>";
713
714 my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
715 " where listorder < 500 and not (type like '_i') order by listorder");
716 $sth2->execute;
717 while (my @types = $sth2->fetchrow_array) {
718 print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
719 " value='$types[0]'>$types[1]</option>\n";
720 }
721
722 print qq(<td><input name=city value="$data[3]"></td>\n).
723 "<td>$data[4]</td><td>$data[5]</td>".
724 qq(<td><input type=submit value="Update"></td></form></tr>\n);
725 }
726 print "</table>\n";
727
728 # notes
729 print "<hr><b>Notes:</b>\n".
730 "<ul>\n<li>Use the main interface to update description and notes fields\n".
731 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
732 "</ul>\n";
733}
734
735
736# Stuff updates into DB
737sub update {
738 eval {
739 # Relatively simple SQL transaction here. Note that we're deliberately NOT
740 # updating notes/desc here as it's available through the main interface.
741 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
742 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
743 $sth->execute;
744 $ip_dbh->commit;
745 };
746 if ($@) {
747 carp "Transaction aborted because $@";
748 eval { $ip_dbh->rollback; };
749 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
750 } else {
751 # If we get here, the operation succeeded.
752 syslog "notice", "$authuser updated $webvar{block}";
753 print "Allocation $webvar{block} updated<hr>\n";
754 }
755 # need to get /24 that block is part of
756 my @bits = split /\./, $webvar{block};
757 $bits[3] = "0/24";
758 showAllocs((join ".", @bits));
759}
760
761
762# showPool()
763# List all IPs in a pool, and allow arbitrary admin changes to each
764# Allow changes to ALL fields
765sub showPool($) {
766 my $pool = new NetAddr::IP $_[0];
767 print qq(Listing pool $pool:\n<table border=1>
768<form action=admin.cgi method=POST>
769<input type=hidden name=action value=updatepool>
770<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
771<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
772<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
773
774 $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
775 $sth->execute;
776 while (my @data = $sth->fetchrow_array) {
777 print "<option value='$data[0]'>$data[1]</option>\n";
778 }
779
780 print qq(</select></td></tr>
781<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
782<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
783<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
784<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
785).
786 "</table>Update the following record:<table border=1>\n";
787 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
788 $sth->execute;
789 while (my @data = $sth->fetchrow_array) {
790 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
791 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
792 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
793 }
794 print "</form></table>\n";
795}
796
797
798# interpret the notify codes
799sub dispNoticeCode {
800 my $code = shift;
801 my $action_out = '';
802
803 if ($code =~ /^s:/) {
804 $code =~ s/^s:/Special: /;
805 return $code;
806 }
807 if ($code =~ /^f:(.+)$/) {
808 $code =~ s/^f://;
809 $action_out = "Failure on ";
810 }
811 if (my $target = $code =~ /^n(.+)/) {
812 $action_out .= "New ";
813 if ($1 eq 'ci') { $action_out .= "city"; }
814 elsif ($1 eq 'no') { $action_out .= "node"; }
815 else { $action_out .= '&lt;unknown&gt;'; }
816 } else {
817 my ($action,$target) = ($code =~ /^(.)(.+)$/);
818 if ($action eq 'a') { $action_out .= 'Add '; }
819 elsif ($action eq 'u') { $action_out .= 'Update '; }
820 elsif ($action eq 'd') { $action_out .= 'Delete '; }
821##fixme: what if we get something funky?
822# What about the eleventy-billion odd combinations possible?
823# this should give an idea of the structure tho
824 if ($target eq 'a') { $action_out .= "all"; }
825 elsif ($target eq '.i') {
826 $action_out .= "all static IPs";
827 }
828 else { $action_out .= $disp_alloctypes{$target}; }
829 }
830 return $action_out;
831}
Note: See TracBrowser for help on using the repository browser.