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

Last change on this file since 906 was 906, checked in by Kris Deugau, 7 years ago

/trunk

Bulk addition of "add 'the directory the script is in' to @INC" for Perls
that have dropped '.' from @INC

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