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

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

/branches/htmlform

Convert dangling DB error to template in admin.cgi; remove all refs to printAndExit().
See #3, #15, #26.

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