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

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

/branches/htmlform

Start converting admin.cgi to templates (to make removal of references to CommonWeb.pm
subs simpler; see #3, #15, #26)
Completed:

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