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

Last change on this file since 541 was 541, checked in by Kris Deugau, 12 years ago

/trunk

Start on SQL in admin.cgi. See #34.

  • Convert Q-n-D allocation list on main page to use existing getTypeList()
  • Convert timestamp-update master block list to use new getMasterList(), with a flag set to return the last-modified time. Also convert main.cgi new assignment page to use this, with the flag set to not return the lastmod.
  • Tweak admin main template to match

While following the code for the master block list, I also removed
several useless globals (@masterblocks, %allocated, %free, and
%routed) since they were only used originally in one place (index
page from main.cgi), obsoleted by changes in r523, and in fact got
overridden locally before that anyway.

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