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