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

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

/branches/htmlform

Merge trunk bugfixes and updates

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