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