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