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

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

/trunk

checkpoint, email notice management. See #21.
Completed delete notice entry.

Still need to add custom code to support legacy notify-on-SWIP.

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