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

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

/branches/htmlform

Continue updating admin.cgi for HTML templates (See #3)
Completed:

  • Touch master timestamp
  • User management (add/remove/change-acl)

Partially stubbed:

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