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

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

/trunk

Renormalized (AKA copy-pasted) existing mailNotify call in admin.cgi
against calls in main.cgi due to incomplete fix in r452. See #2.

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