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