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