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

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

/trunk

Make sure all syslog-using scripts pick up the global logging
facility instead of hardcoding local2. See #17.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 31.8 KB
RevLine 
[54]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.
[65]5#
[54]6###
7# SVN revision info
8# $Date: 2010-07-16 19:56:36 +0000 (Fri, 16 Jul 2010) $
9# SVN revision $Rev: 431 $
10# Last update by $Author: kdeugau $
11###
[417]12# Copyright (C) 2004-2010 - Kris Deugau
[54]13
14use strict;
15use warnings;
16use CGI::Carp qw(fatalsToBrowser);
17use DBI;
18use CommonWeb qw(:ALL);
[400]19use CustIDCK;
[54]20#use POSIX qw(ceil);
21use NetAddr::IP;
22
23use Sys::Syslog;
24
[417]25# don't remove! required for GNU/FHS-ish install from tarball
26##uselib##
27
28use MyIPDB;
29
[431]30openlog "IPDB-admin","pid","$IPDB::syslog_facility";
[54]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
[199]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
[233]53if ($IPDBacl{$authuser} !~ /A/) {
54 print "Content-Type: text/html\n\n".
[413]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".
[415]59 qq(Access to this tool is restricted. Contact the <a href="mailto:ipdbadmin\@example.com">IPDB administrator</a> \n).
[413]60 "for more information.\n</body>\n</html>\n";
[233]61 exit;
62}
63
[54]64my %webvar = parse_post();
65cleanInput(\%webvar);
[58]66
[54]67print "Content-type: text/html\n\n".
[412]68 "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n".
[199]69 qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
[413]70 qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
71 "</head>\n<body>\n".
[54]72 "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
73
74if(!defined($webvar{action})) {
75 $webvar{action} = "<NULL>"; #shuts up the warnings.
[199]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
[329]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
[54]93 print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
94main interface if at all possible.
[199]95<hr>
96<a href="admin.cgi?action=newalloc">Add allocation</a>
97<hr>
98<form action="admin.cgi" method="POST">
[54]99<input type=hidden name=action value=alloc>
[199]100Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
101<input type=submit value=" GIMME!! "></form>
[54]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">
[65]106</form>
[329]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>
[422]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.
[329]115
[65]116<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
[422]117
[258]118<hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users; change
[422]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
[320]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)
[54]127);
128} else {
129 print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
130}
131
[199]132
133## Possible actions.
[54]134if ($webvar{action} eq 'alloc') {
[199]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 '') {
[400]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 ".
[417]157 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
[400]158 "non-customer assignments.");
159 return;
160 }
161 }
[199]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];
[256]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];
[199]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">
[293]231</form>
[199]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 {
[293]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}'";
[422]258 mailNotify($ip_dbh, "a$webvar{alloctype}",
[295]259 "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
260 " $webvar{custid}\n".
[293]261 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
262 }
263 } else {
264 my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
[199]265 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
266 $webvar{circid});
[293]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
[199]277
278 } # done city check
279
[54]280} elsif ($webvar{action} eq 'alloctweak') {
281 fix_allocfrom();
282 showAllocs($webvar{allocfrom});
283} elsif ($webvar{action} eq 'update') {
284 update();
[58]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;
[329]302} elsif ($webvar{action} eq 'touch') {
303 print "Touching master $webvar{whichmaster}\n";
[330]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 }
[329]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>
[418]313<input type=hidden name=action value=edcust>
314<input type=hidden name=newcust value=1>
[329]315<td>CustID:</td><td><input name=custid></td>
[418]316<td align=center><input type=submit value="Go to edit page for this custid"></td></tr>
[329]317</form></table>
318);
[331]319 print "<p>Click CustID to edit existing customer contact data:\n".
[329]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) {
[331]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";
[329]326 }
[331]327 print "</table>\n";
328} elsif ($webvar{action} eq 'edcust') {
[418]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 }
[331]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>
[418]345<tr><td>Street:</td><td><input name=street value="$street"></td>
[331]346<!-- <td>Street2:</td><td><input name=street2></td> -->
[418]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>
[331]352<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
353<td>Description:</td><td><input name=description></td> -->
[418]354<tr><td>Contacts/ARIN Handles:</td><td>
[331]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.
[418]359</td>
360<td>"Special":</td><td><textarea name=special rows=4 cols=40></textarea></td>
361</tr>
[331]362<tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
363</form></table>
[418]364<div style="margin-left:5px">
365<h3>Explanation for "Special" field:</h3>
[422]366This is a temporary place to define the WHOIS "net name" for a block.
[418]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:
[422]371<pre>NetName192.168.236.0/24: MEGAWIDGET-1</pre>
[418]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>
[331]379);
380
[329]381} elsif ($webvar{action} eq 'updcust') {
[418]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
[65]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";
[199]412 $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
[65]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') {
[199]431
[65]432 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
[199]433 "city='$webvar{city}', type='$webvar{type}', available='".
[65]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 }
[258]448} elsif ($webvar{action} eq 'showusers') {
[233]449 print "Notes:<br>\n".
[258]450 "<li>Admin users automatically get all other priviledges.\n".
[320]451 "<li>Everyone has basic read access.\n".
[258]452 "<hr>Add new user:<form action=admin.cgi method=POST>\n".
453 "Username: <input name=username><br>\n".
[259]454 "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
[258]455 "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
[233]456
457 print "<hr>Users with access:\n<table border=1>\n";
[284]458 print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
[233]459 print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
[284]460 "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
[233]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' : '').
[284]472 "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
[233]473 "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
[258]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);
[233]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') {
[284]485 $acl .= "acdsA";
[233]486 } else {
487 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
488 ($webvar{change} eq 'on' ? 'c' : '').
[284]489 ($webvar{del} eq 'on' ? 'd' : '').
490 ($webvar{sysnet} eq 'on' ? 's' : '');
[233]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
[258]498 print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
[233]499
[258]500} elsif ($webvar{action} eq 'newuser') {
501 print "Adding user $webvar{username}...\n";
[259]502 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
503 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
[258]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
[422]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 }
[426]536 print qq(<tr><td colspan=2>Known "special" codes:<br>
537<ul style="margin-top: 0px; margin-bottom: 0px;">
538 <li>swi: Notify if block being updated has SWIP flag set</li>
539</ul></td></tr>
540</table>
541);
[423]542
543# add new entries from this tangle:
[424]544 print "<h4>Add new notification:</h4>\n".
545 "Note: Failure notices on most conditions are not yet supported.\n";
[423]546
547 print qq(<table border=1><form action=admin.cgi method="POST">
548<input type=hidden name=action value=addnotice>
549<tr>
550<td>Recipients</td><td colspan=3><textarea name=reciplist cols=50 rows=5></textarea></td></tr>
551<tr><td>Action</td><td>
552 <input type=radio name=msgaction value=a>Add &nbsp;
553 <input type=radio name=msgaction value=u>Update &nbsp;
554 <input type=radio name=msgaction value=d>Delete
[424]555 <input type=radio name=msgaction value=s:>Special: <input name=special>(requires code changes)
[423]556</td>
557<td>Failure?</td><td><input type=checkbox name=onfail></td></tr>
558<tr><td>Allocation type:</td><td colspan=3>
559 <table>
560 <tr>
561 <td><input type=radio name=alloctype value=a>All allocations</td>
562 <td><input type=radio name=alloctype value=.i>All static IPs</td>
563 </tr>
564 <tr>
565);
566
567 $sth = $ip_dbh->prepare("SELECT type,dispname FROM alloctypes WHERE listorder < 500 ".
568 "ORDER BY listorder");
569 $sth->execute;
570 my $i=0;
571 while (my ($type,$disp) = $sth->fetchrow_array) {
572 print " <td><input type=radio name=alloctype value=$type>$disp</td>";
573 $i++;
574 print " </tr>\n\t<tr>"
575 if ($i % 4 == 0);
576 }
577
578 print qq( </tr>
579 </table>
580</tr>
581<tr><td colspan=4 align=center><input type=submit value="Add notice"></td></tr>
582</table>
583</form>
584);
585 ## done spitting out add-new-spam-me-now table
586
587} elsif ($webvar{action} eq 'addnotice') {
[426]588 $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:';
589 if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) {
590 $webvar{reciplist} =~ s/[\r\n]+/,/g;
591 print "Adding notice to $webvar{reciplist} for ".dispNoticeCode($webvar{msgaction}.$webvar{alloctype}).":\n";
592 $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)");
[423]593##fixme: automagically merge reciplists iff action already exists
[426]594 $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
595 $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist});
596 if ($sth->err) {
597 print "Failed: DB error: ".$sth->errstr."\n";
598 } else {
599 print "OK!<br>\n"
600 }
[423]601 } else {
[426]602 print "Need to specify at least one recipient, an action, and an allocation type. ".
603 qq{("Special" content is considered an allocation type). Hit the Back button and try again.<br>\n};
[423]604 }
605 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
606
[424]607} elsif ($webvar{action} eq 'delnotice') {
608 print "Deleting notices on ".dispNoticeCode($webvar{code}.$webvar{alloctype}).":\n";
609 $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?");
610 $sth->execute($webvar{code});
611 if ($sth->err) {
612 print "Failed: DB error: ".$sth->errstr."\n";
613 } else {
614 print "OK!<br>\n"
615 }
616 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
617
[422]618} elsif ($webvar{action} eq 'ednotice') {
619 print "<h4>Editing recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
620 $sth = $ip_dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
621 $sth->execute($webvar{code});
622 my ($reciplist) = $sth->fetchrow_array;
623 $reciplist =~ s/,/\n/g;
624 print qq(<form action=admin.cgi method=POST><input type=hidden name=code value="$webvar{code}">\n).
625 qq(<input type=hidden name=action value="updnotice"><table border=1><tr><td>).
626 qq(<textarea cols="40" rows="5" name=reciplist>$reciplist</textarea></td><td><input type=submit value="Update">\n).
627 "</td></tr></table></form>\n";
628} elsif ($webvar{action} eq 'updnotice') {
629 print "<h4>Updating recipient list for '".dispNoticeCode($webvar{code})."':</h4>\n";
630 $sth = $ip_dbh->prepare("UPDATE notify SET reciplist=? WHERE action=?");
631 $webvar{reciplist} =~ s/[\r\n]+/,/g;
632 $sth->execute($webvar{reciplist}, $webvar{code});
633 if ($sth->err) {
634 print "Failed: DB error: ".$sth->errstr."\n";
635 } else {
636 print "OK!<br>\n"
637 }
638 print qq(<a href="admin.cgi?action=emailnotice">Back to email notice list</a>\n);
[256]639} elsif ($webvar{action} ne '<NULL>') {
[233]640 print "webvar{action} check failed: Don't know how to $webvar{action}";
[54]641}
642
643# Hokay. This is a little different. We have a few specific functions here:
644# -> Assign arbitrary subnet from arbitrary free space
645# -> Tweak individual DB fields
646#
647
[400]648print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n);
[54]649
650printFooter;
651
652$ip_dbh->disconnect;
653
654exit;
655
656
657# Tweak allocfrom into shape.
658sub fix_allocfrom {
659 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
660 # 3-octet class C specified
661 $webvar{allocfrom} .= ".0/24";
662 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
663 # 4-octet IP specified;
664 $webvar{allocfrom} .= "/24";
665 }
666}
667
668
[58]669# List free blocks in a /24 for arbitrary manual allocation
670sub showfree($) {
671 my $cidr = new NetAddr::IP $_[0];
672 print "Showing free blocks in $cidr<br>\n".
673 "<table border=1>\n";
674 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
675 $sth->execute;
676 while (my @data = $sth->fetchrow_array) {
677 my $temp = new NetAddr::IP $data[0];
678 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
679 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
680 "<td>".
681 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
682 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
683 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
684 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
685 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
686 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
687 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
688 "</td>".
689 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
690 "\n</form></tr>\n";
691 }
692 print "</table>\n";
693}
694
695
[54]696# Show allocations to allow editing.
697sub showAllocs($) {
698 my $cidr = new NetAddr::IP $_[0];
699 print "Edit custID, allocation type, city for allocations in ".
700 "$cidr:\n<table border=1>";
701 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
702 $sth->execute;
703 while (my @data = $sth->fetchrow_array) {
704 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
705 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
[348]706 qq(<td><input name=custid value="$data[1]"></td>\n).
707 "<td><select name=alloctype>";
[54]708
[348]709 my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
710 " where listorder < 500 and not (type like '_i') order by listorder");
711 $sth2->execute;
712 while (my @types = $sth2->fetchrow_array) {
713 print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
714 " value='$types[0]'>$types[1]</option>\n";
715 }
716
[54]717 print qq(<td><input name=city value="$data[3]"></td>\n).
718 "<td>$data[4]</td><td>$data[5]</td>".
719 qq(<td><input type=submit value="Update"></td></form></tr>\n);
720 }
721 print "</table>\n";
722
723 # notes
724 print "<hr><b>Notes:</b>\n".
725 "<ul>\n<li>Use the main interface to update description and notes fields\n".
726 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
727 "</ul>\n";
728}
729
730
731# Stuff updates into DB
732sub update {
733 eval {
734 # Relatively simple SQL transaction here. Note that we're deliberately NOT
735 # updating notes/desc here as it's available through the main interface.
736 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
737 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
738 $sth->execute;
739 $ip_dbh->commit;
740 };
741 if ($@) {
742 carp "Transaction aborted because $@";
743 eval { $ip_dbh->rollback; };
[65]744 syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
[54]745 } else {
746 # If we get here, the operation succeeded.
747 syslog "notice", "$authuser updated $webvar{block}";
748 print "Allocation $webvar{block} updated<hr>\n";
749 }
750 # need to get /24 that block is part of
751 my @bits = split /\./, $webvar{block};
752 $bits[3] = "0/24";
753 showAllocs((join ".", @bits));
754}
[65]755
756
757# showPool()
758# List all IPs in a pool, and allow arbitrary admin changes to each
759# Allow changes to ALL fields
760sub showPool($) {
761 my $pool = new NetAddr::IP $_[0];
762 print qq(Listing pool $pool:\n<table border=1>
763<form action=admin.cgi method=POST>
764<input type=hidden name=action value=updatepool>
765<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
766<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
[348]767<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
768
769 $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
770 $sth->execute;
771 while (my @data = $sth->fetchrow_array) {
772 print "<option value='$data[0]'>$data[1]</option>\n";
773 }
774
775 print qq(</select></td></tr>
[65]776<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
777<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
778<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
779<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
780).
781 "</table>Update the following record:<table border=1>\n";
[199]782 $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
[65]783 $sth->execute;
784 while (my @data = $sth->fetchrow_array) {
785 print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
786 "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
787 "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
788 }
789 print "</form></table>\n";
790}
[422]791
792
793# interpret the notify codes
794sub dispNoticeCode {
795 my $code = shift;
796 my $action_out = '';
[426]797
798 if ($code =~ /^s:/) {
799 $code =~ s/^s:/Special: /;
800 return $code;
801 }
[422]802 if ($code =~ /^f:(.+)$/) {
803 $code =~ s/^f://;
804 $action_out = "Failure on ";
805 }
806 if (my $target = $code =~ /^n(.+)/) {
807 $action_out .= "New ";
808 if ($1 eq 'ci') { $action_out .= "city"; }
809 elsif ($1 eq 'no') { $action_out .= "node"; }
810 else { $action_out .= '&lt;unknown&gt;'; }
811 } else {
812 my ($action,$target) = ($code =~ /^(.)(.+)$/);
813 if ($action eq 'a') { $action_out .= 'Add '; }
814 elsif ($action eq 'u') { $action_out .= 'Update '; }
815 elsif ($action eq 'd') { $action_out .= 'Delete '; }
816##fixme: what if we get something funky?
[423]817# What about the eleventy-billion odd combinations possible?
818# this should give an idea of the structure tho
[422]819 if ($target eq 'a') { $action_out .= "all"; }
[423]820 elsif ($target =~ /^\.i$/) {
821 $action_out .= "all static IPs";
822 }
[422]823 else { $action_out .= $disp_alloctypes{$target}; }
824 }
825 return $action_out;
826}
Note: See TracBrowser for help on using the repository browser.