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

Last change on this file since 940 was 929, checked in by Kris Deugau, 2 years ago

/trunk

Trim errant whitespace from "gimme" CIDR in admin.cgi.
Update copyright dates

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