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