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

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

/branches/htmlform

Finish IP pool tweaking
Complete email notice configuration
Finish most of block allocation
Add generic error page
Fix admin header with reference to Javascript bits
See #3, #15, #26

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 21.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-09-23 05:10:39 +0000 (Thu, 23 Sep 2010) $
9# SVN revision $Rev: 487 $
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
[487]86my $page;
87if (-e "$ENV{HTML_TEMPLATE_ROOT}/admin/$webvar{action}.tmpl") {
88 $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl");
89} else {
90 $page = HTML::Template->new(filename => "admin/dunno.tmpl");
91}
[483]92
93if ($webvar{action} eq 'main') {
94 $header->param(mainpage => 1);
95
[199]96 $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
97 $sth->execute;
[483]98
99 my @typelist;
100 my $count = 0;
101 while (my ($type,$listname) = $sth->fetchrow_array) {
102 my %row = (
103 selected => $count++,
104 type => $type,
105 dispname => $listname
106 );
107 push @typelist, \%row;
[199]108 }
[483]109 $page->param(typelist => \@typelist);
[199]110
[483]111 my @masterlist;
[329]112 $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr");
113 $sth->execute;
[483]114 while (my ($cidr,$mtime) = $sth->fetchrow_array) {
115 my %row = (
116 master => $cidr,
117 masterdate => $mtime
118 );
119 push @masterlist, \%row;
[329]120 }
[483]121 $page->param(masterlist => \@masterlist);
[329]122
[54]123}
124
[484]125## Non-default actions.
[483]126elsif ($webvar{action} eq 'alloc') {
[199]127 # OK, we know what we're allocating.
128
129 if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
130 printAndExit("Can't allocate something that's not a netblock/ip");
131 }
132
133 $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
134 $sth->execute;
135 my @data = $sth->fetchrow_array;
136 my $custid = $data[0];
137 if ($custid eq '') {
[400]138 if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
139 # Force uppercase for now...
140 $webvar{custid} =~ tr/a-z/A-Z/;
141 # Crosscheck with billing.
142 my $status = CustIDCK->custid_exist($webvar{custid});
143 if ($CustIDCK::Error) {
144 printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
145 return;
146 }
147 if (!$status) {
148 printError("Customer ID not valid. Make sure the Customer ID ".
[417]149 "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
[400]150 "non-customer assignments.");
151 return;
152 }
153 }
[199]154 # Type that doesn't have a default custid
155 $custid = $webvar{custid};
156 }
157
158 my $cidr = new NetAddr::IP $webvar{cidr};
159 my @data;
160 if ($webvar{alloctype} eq 'rm') {
161 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
162 $sth->execute;
163 @data = $sth->fetchrow_array;
164# User deserves errors if user can't be bothered to find the free block first.
165 printAndExit("Can't allocate from outside a free block!!\n")
166 if !$data[0];
[256]167 } elsif ($webvar{alloctype} =~ /^(.)i$/) {
168 $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
169 $sth->execute;
170 @data = $sth->fetchrow_array;
171# User deserves errors if user can't be bothered to find the pool and a free IP first.
172 printAndExit("Can't allocate static IP from outside a pool!!\n")
173 if !$data[0];
[199]174 } else {
175 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
176 $sth->execute;
177 @data = $sth->fetchrow_array;
178# User deserves errors if user can't be bothered to find the free block first.
179 printAndExit("Can't allocate from outside a routed block!!\n")
180 if !$data[0];
181 }
182
183 my $alloc_from = new NetAddr::IP $data[0];
184 $sth->finish;
185
[487]186 my @cities;
[199]187 foreach my $city (@citylist) {
[487]188 my %row = (city => $city);
189 push @cities, \%row;
[199]190 }
[487]191 $page->param(
192 cidr => $cidr,
193 disptype => $disp_alloctypes{$webvar{alloctype}},
194 type => $webvar{alloctype},
195 alloc_from => $alloc_from,
196 custid => $custid,
197 citylist => \@cities
198 );
[199]199
200} elsif ($webvar{action} eq 'confirm') {
201
[487]202 $page->param(
203 cidr => $webvar{cidr},
204 custid => $webvar{custid},
205 desc => $webvar{desc},
206 disptype => $disp_alloctypes{$webvar{alloctype}}
207 );
[199]208 # Only need to check city here.
209 if ($webvar{city} eq '-') {
210 printError("Invalid customer location! Go back and select customer's location.");
211 } else {
[293]212 if ($webvar{alloctype} =~ /^.i$/) {
213 $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ".
214 "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ".
215 "where ip='$webvar{cidr}'");
216 $sth->execute;
217 if ($sth->err) {
[487]218 $page->param(errmsg => $sth->errstr);
[293]219 syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
220 "'$webvar{alloctype}' failed: '".$sth->errstr."'";
221 } else {
222 syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
223 "'$webvar{alloctype}'";
[468]224 mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
225 "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
[293]226 "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
227 }
228 } else {
229 my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
[199]230 $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
231 $webvar{circid});
[293]232 if ($retcode eq 'OK') {
233 syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
234 "'$webvar{alloctype}'";
235 } else {
[487]236 $page->param(errmsg => $msg);
[293]237 syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
238 "'$webvar{alloctype}' failed: '$msg'";
239 }
240 } # static IP vs netblock
[199]241
242 } # done city check
243
[54]244} elsif ($webvar{action} eq 'alloctweak') {
245 fix_allocfrom();
246 showAllocs($webvar{allocfrom});
247} elsif ($webvar{action} eq 'update') {
248 update();
[58]249} elsif ($webvar{action} eq 'assign') {
250 # Display a list of possible blocks within the requested block.
251 open (HTML, "../admin_alloc.html")
252 or croak "Could not open admin_alloc.html :$!";
253 my $html = join('', <HTML>);
254 $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
255 $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
256
257 my $from = new NetAddr::IP $webvar{allocfrom};
258 my @blocklist = $from->split($webvar{masklen});
259 my $availblocks;
260 foreach (@blocklist) {
261 $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
262 }
263 $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
264
265 print $html;
[484]266
[329]267} elsif ($webvar{action} eq 'touch') {
[484]268
269 $page->param(master => $webvar{whichmaster});
[330]270 $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
271 $sth->execute;
272 if ($sth->err) {
[484]273 $page->param(errmsg => $sth->errstr);
[330]274 }
[483]275
[329]276} elsif ($webvar{action} eq 'listcust') {
[483]277
[329]278 $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
279 $sth->execute;
[483]280 my @custlist;
[329]281 while (my @data = $sth->fetchrow_array) {
[483]282 my %row = (
283 custid => $data[0],
284 custname => $data[1],
285 tech => $data[2]
286 );
287 push @custlist, \%row;
[329]288 }
[483]289 $page->param(custlist => \@custlist);
290
[331]291} elsif ($webvar{action} eq 'edcust') {
[483]292
[418]293 if ($webvar{newcust}) {
294 $sth = $ip_dbh->prepare("INSERT INTO customers (custid) VALUES (?)");
295 $sth->execute($webvar{custid});
296 }
[331]297 $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
[436]298 "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ".
[331]299 "from customers where custid='$webvar{custid}'");
300 $sth->execute;
[436]301 my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) =
[331]302 $sth->fetchrow_array;
303
[483]304 $page->param(
305 custid => $custid,
306 name => $name,
307 street => $street,
308 city => $city,
309 prov => $prov,
310 country => $country,
311 pocode => $pocode,
312 phone => $phone,
313 tech => $tech,
314 abuse => $abuse,
315 admin => $admin,
316 special => $special
317 );
318
[329]319} elsif ($webvar{action} eq 'updcust') {
[483]320
[418]321 $sth = $ip_dbh->prepare("UPDATE customers SET".
322 " name=?, street=?, city=?, province=?, country=?, pocode=?,".
323 " phone=?, tech_handle=?, abuse_handle=?, admin_handle=?, special=?".
324 " WHERE custid=?");
325 $sth->execute($webvar{name}, $webvar{street}, $webvar{city}, $webvar{province},
326 $webvar{country}, $webvar{pocode}, $webvar{phone}, $webvar{tech_handle},
327 $webvar{abuse_handle}, $webvar{admin_handle}, $webvar{special}, $webvar{custid});
[483]328 $page->param(
329 custid => $webvar{custid},
330 name => $webvar{name},
331 street => $webvar{street},
332 city => $webvar{city},
333 prov => $webvar{province},
334 country => $webvar{country},
335 pocode => $webvar{pocode},
336 phone => $webvar{phone},
337 tech => $webvar{tech_handle},
338 abuse => $webvar{abuse_handle},
339 admin => $webvar{admin_handle},
340 special => $webvar{special}
341 );
[418]342
[65]343} elsif ($webvar{action} eq 'showpools') {
[484]344
345 $sth = $ip_dbh->prepare("select pool, count(*) from poolips where available='y' group by pool order by pool");
[65]346 $sth->execute;
[484]347 my @poollist;
348 while (my ($pool,$free) = $sth->fetchrow_array) {
349 my %row = (
350 pool => $pool,
351 free => $free
352 );
353 push @poollist, \%row;
[65]354 }
[484]355 $page->param(poollist => \@poollist);
356
[65]357} elsif ($webvar{action} eq 'tweakpool') {
[487]358
[65]359 showPool($webvar{pool});
[487]360
[65]361} elsif ($webvar{action} eq 'updatepool') {
[199]362
[65]363 $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
[484]364 "city=?, type='$webvar{type}', available='".
[65]365 (($webvar{available} eq 'y') ? 'y' : 'n').
[484]366 "', notes=?, description=? ".
[65]367 "where ip='$webvar{ip}'");
[484]368 $sth->execute($webvar{city},$webvar{notes},$webvar{desc});
[487]369 $page->param(ip => $webvar{ip});
[65]370 if ($sth->err) {
[487]371 $page->param(errmsg => $sth->errstr);
372 syslog "err", "$authuser could not update pool IP $webvar{ip}: ".$sth->errstr;
373 } else {
[65]374 syslog "notice", "$authuser updated pool IP $webvar{ip}";
375 }
[487]376 $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
377 $sth->execute;
378 my @data = $sth->fetchrow_array;
379 $page->param(pool => $data[0]);
[484]380
[258]381} elsif ($webvar{action} eq 'showusers') {
[233]382
383 $sth = $ip_dbh->prepare("select username,acl from users order by username");
384 $sth->execute;
[484]385 my @userlist;
386 while (my ($username,$acl) = $sth->fetchrow_array) {
387##fixme: funky things happening with HTML::Template here; shouldn't need the "logic ? iftrue : iffalse" structure
388 my %row = (
389 username => $username,
390 can_add => ($acl =~ /a/ ? 1 : 0),
391 can_change => ($acl =~ /c/ ? 1 : 0),
392 can_del => ($acl =~ /d/ ? 1 : 0),
393 sysnet => ($acl =~ /s/ ? 1 : 0),
394 is_admin => ($acl =~ /A/ ? 1 : 0),
395 acl => $acl
396 );
397 push @userlist, \%row;
398 }
399 $page->param(userlist => \@userlist);
[233]400
401} elsif ($webvar{action} eq 'updacl') {
[484]402
403 $page->param(username => $webvar{username});
[233]404 my $acl = 'b';
405 if ($webvar{admin} eq 'on') {
[284]406 $acl .= "acdsA";
[233]407 } else {
408 $acl .= ($webvar{add} eq 'on' ? 'a' : '').
409 ($webvar{change} eq 'on' ? 'c' : '').
[284]410 ($webvar{del} eq 'on' ? 'd' : '').
411 ($webvar{sysnet} eq 'on' ? 's' : '');
[233]412 }
[484]413 $page->param(acl => $acl);
[233]414
415 $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
416 $sth->execute;
[484]417 $page->param(errmsg => $sth->errstr) if $sth->err;
[233]418
[484]419} elsif ($webvar{action} eq 'newuser') {
[233]420
[484]421 $page->param(username => $webvar{username});
[259]422 my $cr_pass = ($webvar{preenc} ? $webvar{password} :
423 crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
[258]424 $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
425 "('$webvar{username}','$cr_pass','b')");
426 $sth->execute;
[484]427 $page->param(errmsg => $sth->errstr) if $sth->err;
[258]428
[484]429} elsif ($webvar{action} eq 'deluser') {
[258]430
[484]431 $page->param(username => $webvar{username});
[258]432 $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
433 $sth->execute;
[484]434 $page->param(errmsg => $sth->errstr) if $sth->err;
[258]435
[422]436} elsif ($webvar{action} eq 'emailnotice') {
[487]437
[422]438 $sth = $ip_dbh->prepare("SELECT action,reciplist FROM notify");
439 $sth->execute;
[487]440 my @spamlist;
[422]441 while (my ($notice_code,$reciplist) = $sth->fetchrow_array() ) {
442##fixme: hairy mess, only a few things call mailNotify() anyway, so many possible notices won't work.
443 my $action_out = dispNoticeCode($notice_code);
[487]444 my %row = (
445 action => $action_out,
446 code => $notice_code,
447 recips => $reciplist
448 );
449 push @spamlist, \%row;
[422]450 }
[487]451 $page->param(spamlist => \@spamlist);
[423]452
453 $sth = $ip_dbh->prepare("SELECT type,dispname FROM alloctypes WHERE listorder < 500 ".
454 "ORDER BY listorder");
455 $sth->execute;
456 my $i=0;
[487]457 my @typelist;
[423]458 while (my ($type,$disp) = $sth->fetchrow_array) {
[487]459 my %row = (
460 type => $type,
461 disptype => $disp,
462# ahh, off-by-one counts, how we do love thee... NOT!
463 newrow => ($i+2 > $sth->rows ? 1 : (++$i % 4)),
464 );
465 push @typelist, \%row;
[423]466 }
[487]467 $page->param(typelist => \@typelist);
[423]468
[487]469} elsif ($webvar{action} eq 'addnotice') {
[423]470
[426]471 $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:';
472 if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) {
[487]473 $page->param(cantry => 1);
[426]474 $webvar{reciplist} =~ s/[\r\n]+/,/g;
[438]475 $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
[487]476 $page->param(reciplist => $webvar{reciplist});
477 $page->param(dispnotice => dispNoticeCode($webvar{msgaction}.$webvar{alloctype}));
[426]478 $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)");
[423]479##fixme: automagically merge reciplists iff action already exists
[426]480 $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist});
[487]481 $page->param(addfailed => $sth->errstr) if $sth->err;
[423]482 }
483
[424]484} elsif ($webvar{action} eq 'delnotice') {
[487]485
486 $page->param(dispnotice => dispNoticeCode($webvar{code}.$webvar{alloctype}));
[424]487 $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?");
488 $sth->execute($webvar{code});
[487]489 $page->param(delfailed => $sth->errstr) if $sth->err;
[424]490
[422]491} elsif ($webvar{action} eq 'ednotice') {
[487]492
493 $page->param(dispnotice => dispNoticeCode($webvar{code}));
494 $page->param(code => $webvar{code});
[422]495 $sth = $ip_dbh->prepare("SELECT reciplist FROM notify WHERE action=?");
496 $sth->execute($webvar{code});
497 my ($reciplist) = $sth->fetchrow_array;
498 $reciplist =~ s/,/\n/g;
[487]499 $page->param(reciplist => $reciplist);
500
[422]501} elsif ($webvar{action} eq 'updnotice') {
[487]502
503 $page->param(dispnotice => dispNoticeCode($webvar{code}));
[422]504 $sth = $ip_dbh->prepare("UPDATE notify SET reciplist=? WHERE action=?");
505 $webvar{reciplist} =~ s/[\r\n]+/,/g;
506 $sth->execute($webvar{reciplist}, $webvar{code});
[487]507 $page->param(updfailed => $sth->errstr) if $sth->err;
508
[256]509} elsif ($webvar{action} ne '<NULL>') {
[487]510 $page->param(dunno => $webvar{action});
[54]511}
512
[483]513print "Content-type: text/html\n\n".$header->output;
514print $page->output;
[54]515
[483]516##fixme: make me a footer param!
517print qq(<hr><div><a href="/ip/">Back</a> to main interface</div>\n);
[54]518
[449]519# We print the footer here, so we don't have to do it elsewhere.
520my $footer = HTML::Template->new(filename => "footer.tmpl");
521# we're already in the admin tools, no need to provide a bottom link. maybe.
522#$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
[54]523
[449]524print $footer->output;
525
[54]526$ip_dbh->disconnect;
527
528exit;
529
530
[483]531# Hokay. This is a little different. We have a few specific functions here:
532# -> Assign arbitrary subnet from arbitrary free space
533# -> Tweak individual DB fields
534#
535
536
[54]537# Tweak allocfrom into shape.
538sub fix_allocfrom {
539 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
540 # 3-octet class C specified
541 $webvar{allocfrom} .= ".0/24";
542 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
543 # 4-octet IP specified;
544 $webvar{allocfrom} .= "/24";
545 }
546}
547
548
[58]549# List free blocks in a /24 for arbitrary manual allocation
550sub showfree($) {
551 my $cidr = new NetAddr::IP $_[0];
552 print "Showing free blocks in $cidr<br>\n".
553 "<table border=1>\n";
554 $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
555 $sth->execute;
556 while (my @data = $sth->fetchrow_array) {
557 my $temp = new NetAddr::IP $data[0];
558 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
559 qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
560 "<td>".
561 (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
562 : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
563 (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
564 (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
565 (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
566 (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
567 (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
568 "</td>".
569 qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
570 "\n</form></tr>\n";
571 }
572 print "</table>\n";
573}
574
575
[54]576# Show allocations to allow editing.
[487]577sub showAllocs {
578
579 my $within = new NetAddr::IP $_[0];
580 $page->param(within => $within);
581
582 $sth = $ip_dbh->prepare("select cidr,custid,type,city,description from allocations where cidr <<= '$within' order by cidr");
[54]583 $sth->execute;
[487]584 my @blocklist;
585 while (my ($cidr,$custid,$type,$city,$desc) = $sth->fetchrow_array) {
586 my %row = (
587 cidr => $cidr,
588 custid => $custid,
589 city => $city,
590 desc => $desc,
591 );
[54]592
[487]593##fixme: don't wanna retrieve the whole type list *every time around the outer loop*
[348]594 my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
595 " where listorder < 500 and not (type like '_i') order by listorder");
596 $sth2->execute;
[487]597 my @typelist;
598 while (my ($listtype,$dispname) = $sth2->fetchrow_array) {
599 my %subrow = (
600 type => $listtype,
601 dispname => $dispname,
602 selected => ($listtype eq $type)
603 );
604 push @typelist, \%subrow;
[348]605 }
[487]606 $row{typelist} = \@typelist;
607 push @blocklist, \%row;
[54]608 }
[487]609 $page->param(blocklist => \@blocklist);
610} # end showAllocs()
[54]611
612
613# Stuff updates into DB
614sub update {
[487]615 # Relatively simple SQL transaction here. Note that we're deliberately NOT
616 # updating notes/desc here as it's available through the main interface.
617 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
618 "city=?,type='$webvar{alloctype}' where cidr='$webvar{block}'");
619 $sth->execute($webvar{city});
620
621 $page->param(block => $webvar{block});
622 if ($sth->err) {
623 $page->param(updfailed => $sth->errstr);
624 syslog "err", "$authuser could not update block '$webvar{block}': '".$sth->errstr."'";
[54]625 } else {
626 syslog "notice", "$authuser updated $webvar{block}";
627 }
628 # need to get /24 that block is part of
629 my @bits = split /\./, $webvar{block};
630 $bits[3] = "0/24";
631 showAllocs((join ".", @bits));
[487]632} # end update()
[65]633
634
635# showPool()
636# List all IPs in a pool, and allow arbitrary admin changes to each
637# Allow changes to ALL fields
638sub showPool($) {
639 my $pool = new NetAddr::IP $_[0];
[348]640
641 $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
642 $sth->execute;
[487]643 my @typelist;
644 while (my ($type,$dispname) = $sth->fetchrow_array) {
645 my %row = (
646 type => $type,
647 dispname => $dispname
648 );
649 push @typelist, \%row;
[348]650 }
[487]651 $page->param(typelist => \@typelist);
[348]652
[487]653 $sth = $ip_dbh->prepare("select ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
[65]654 $sth->execute;
[487]655 my @iplist;
656 while (my ($ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) {
657 my %row = (
658 ip => $ip,
659 custid => $custid,
660 city => $city,
661 type => $type,
662 avail => $avail,
663 desc => $desc,
664 notes => $notes
665 );
666 push @iplist, \%row;
[65]667 }
[487]668 $page->param(iplist => \@iplist);
669} # end showPool()
[422]670
671
672# interpret the notify codes
673sub dispNoticeCode {
674 my $code = shift;
675 my $action_out = '';
[426]676
677 if ($code =~ /^s:/) {
678 $code =~ s/^s:/Special: /;
679 return $code;
680 }
[422]681 if ($code =~ /^f:(.+)$/) {
682 $code =~ s/^f://;
683 $action_out = "Failure on ";
684 }
685 if (my $target = $code =~ /^n(.+)/) {
686 $action_out .= "New ";
687 if ($1 eq 'ci') { $action_out .= "city"; }
688 elsif ($1 eq 'no') { $action_out .= "node"; }
689 else { $action_out .= '&lt;unknown&gt;'; }
690 } else {
691 my ($action,$target) = ($code =~ /^(.)(.+)$/);
692 if ($action eq 'a') { $action_out .= 'Add '; }
693 elsif ($action eq 'u') { $action_out .= 'Update '; }
694 elsif ($action eq 'd') { $action_out .= 'Delete '; }
695##fixme: what if we get something funky?
[423]696# What about the eleventy-billion odd combinations possible?
697# this should give an idea of the structure tho
[422]698 if ($target eq 'a') { $action_out .= "all"; }
[438]699 elsif ($target eq '.i') {
[423]700 $action_out .= "all static IPs";
701 }
[422]702 else { $action_out .= $disp_alloctypes{$target}; }
703 }
704 return $action_out;
705}
Note: See TracBrowser for help on using the repository browser.