[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: 2012-11-07 22:20:58 +0000 (Wed, 07 Nov 2012) $
|
---|
| 9 | # SVN revision $Rev: 545 $
|
---|
| 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
|
---|
| 57 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates";
|
---|
| 58 |
|
---|
[199] | 59 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
| 60 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
| 61 | my $ip_dbh;
|
---|
| 62 | my $sth;
|
---|
| 63 | my $errstr;
|
---|
| 64 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
| 65 | if (!$ip_dbh) {
|
---|
[517] | 66 | $webvar{action} = "dberr";
|
---|
| 67 | } else {
|
---|
| 68 | initIPDBGlobals($ip_dbh);
|
---|
[199] | 69 | }
|
---|
| 70 |
|
---|
[517] | 71 | # handle DB error output
|
---|
| 72 | if ($webvar{action} eq 'dberr') {
|
---|
| 73 | my $page = HTML::Template->new(filename => "admin/dberr.tmpl");
|
---|
| 74 | $page->param(errmsg => $errstr);
|
---|
| 75 | print "Content-Type: text/html\n\n".$page->output;
|
---|
| 76 | exit;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[233] | 79 | if ($IPDBacl{$authuser} !~ /A/) {
|
---|
[517] | 80 | my $page = HTML::Template->new(filename => "admin/aclerr.tmpl");
|
---|
| 81 | ##fixme: need params for IPDB admin email and name
|
---|
| 82 | $page->param(ipdbadmin_email => 'ipdbadmin@example.com');
|
---|
| 83 | $page->param(ipdbadmin_name => 'the IPDB administrator');
|
---|
| 84 | print "Content-Type: text/html\n\n".$page->output;
|
---|
[233] | 85 | exit;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[517] | 88 | my $header = HTML::Template->new(filename => "admin/header.tmpl");
|
---|
[58] | 89 |
|
---|
[54] | 90 | if(!defined($webvar{action})) {
|
---|
[517] | 91 | $webvar{action} = "main"; #shuts up the warnings.
|
---|
| 92 | }
|
---|
[199] | 93 |
|
---|
[517] | 94 | my $page;
|
---|
| 95 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/admin/$webvar{action}.tmpl") {
|
---|
| 96 | $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl");
|
---|
| 97 | } else {
|
---|
| 98 | $page = HTML::Template->new(filename => "admin/dunno.tmpl");
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | # handle index page
|
---|
| 102 | if ($webvar{action} eq 'main') {
|
---|
| 103 | $header->param(mainpage => 1);
|
---|
| 104 |
|
---|
[541] | 105 | my $tlist = getTypeList($ip_dbh, 'a');
|
---|
| 106 | $tlist->[0]->{sel} = 1;
|
---|
| 107 | $page->param(typelist => $tlist);
|
---|
[199] | 108 |
|
---|
[541] | 109 | my $mlist = getMasterList($ip_dbh, 'm');
|
---|
| 110 | $page->param(masterlist => $mlist);
|
---|
[54] | 111 | }
|
---|
| 112 |
|
---|
[517] | 113 | ## Non-default actions.
|
---|
[199] | 114 |
|
---|
[517] | 115 | elsif ($webvar{action} eq 'alloc') {
|
---|
[199] | 116 |
|
---|
[543] | 117 | my $cidr = new NetAddr::IP $webvar{cidr};
|
---|
| 118 | if (!$cidr || "$cidr" =~ /^0/) {
|
---|
[517] | 119 | $page->param(errmsg => "Can't allocate something that's not a netblock/ip");
|
---|
| 120 | goto ERRJUMP;
|
---|
[199] | 121 | }
|
---|
| 122 |
|
---|
[544] | 123 | my $custid = $def_custids{$webvar{alloctype}};
|
---|
[199] | 124 | if ($custid eq '') {
|
---|
[400] | 125 | if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
|
---|
| 126 | # Force uppercase for now...
|
---|
| 127 | $webvar{custid} =~ tr/a-z/A-Z/;
|
---|
| 128 | # Crosscheck with billing.
|
---|
| 129 | my $status = CustIDCK->custid_exist($webvar{custid});
|
---|
| 130 | if ($CustIDCK::Error) {
|
---|
[517] | 131 | $page->param(errmsg => "Error verifying customer ID: ".$CustIDCK::ErrMsg);
|
---|
| 132 | goto ERRJUMP;
|
---|
[400] | 133 | }
|
---|
| 134 | if (!$status) {
|
---|
[517] | 135 | $page->param(errmsg => "Customer ID not valid. Make sure the Customer ID ".
|
---|
[417] | 136 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ".
|
---|
[400] | 137 | "non-customer assignments.");
|
---|
[517] | 138 | goto ERRJUMP;
|
---|
[400] | 139 | }
|
---|
| 140 | }
|
---|
[199] | 141 | # Type that doesn't have a default custid
|
---|
| 142 | $custid = $webvar{custid};
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[544] | 145 | my $maskbits = $cidr->masklen;
|
---|
| 146 | my $fbtmp = findAllocateFrom($ip_dbh, $maskbits, $webvar{alloctype}, '','',
|
---|
| 147 | (gimme => "$cidr", allowpriv => 1));
|
---|
| 148 |
|
---|
[199] | 149 | if ($webvar{alloctype} eq 'rm') {
|
---|
[544] | 150 | if (!$fbtmp) {
|
---|
[517] | 151 | $page->param(errmsg => "Can't allocate from outside a free block!!");
|
---|
| 152 | goto ERRJUMP;
|
---|
| 153 | }
|
---|
[256] | 154 | } elsif ($webvar{alloctype} =~ /^(.)i$/) {
|
---|
[544] | 155 | my $iptype = $1;
|
---|
| 156 | my $ptmp = ipParent($ip_dbh, "$cidr");
|
---|
| 157 | if ($ptmp->{type} =~ /^(.)[dp]$/) {
|
---|
| 158 | my $newiptype = "$1i";
|
---|
| 159 | $fbtmp = $ptmp->{cidr};
|
---|
| 160 | if ($ptmp->{type} !~ /^$iptype./) {
|
---|
| 161 | $page->param(warnmsg => "Warning: Allocating IP as '".$disp_alloctypes{$newiptype}."' instead of '".
|
---|
| 162 | $disp_alloctypes{$webvar{alloctype}}."' to match pool $fbtmp\n");
|
---|
| 163 | $webvar{alloctype} = $newiptype;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | if (!$fbtmp) {
|
---|
[517] | 167 | $page->param(errmsg => "Can't allocate static IP from outside a pool!!");
|
---|
| 168 | goto ERRJUMP;
|
---|
| 169 | }
|
---|
[199] | 170 | } else {
|
---|
[544] | 171 | if (!$fbtmp) {
|
---|
[517] | 172 | $page->param(errmsg => "Can't allocate from outside a routed block!!");
|
---|
| 173 | goto ERRJUMP;
|
---|
| 174 | }
|
---|
[199] | 175 | }
|
---|
| 176 |
|
---|
[544] | 177 | my $alloc_from = new NetAddr::IP $fbtmp;
|
---|
[199] | 178 |
|
---|
[517] | 179 | my @cities;
|
---|
[199] | 180 | foreach my $city (@citylist) {
|
---|
[517] | 181 | my %row = (city => $city);
|
---|
| 182 | push @cities, \%row;
|
---|
[199] | 183 | }
|
---|
[517] | 184 | $page->param(
|
---|
| 185 | cidr => $cidr,
|
---|
| 186 | disptype => $disp_alloctypes{$webvar{alloctype}},
|
---|
| 187 | type => $webvar{alloctype},
|
---|
| 188 | alloc_from => $alloc_from,
|
---|
| 189 | custid => $custid,
|
---|
| 190 | citylist => \@cities
|
---|
| 191 | );
|
---|
[199] | 192 |
|
---|
| 193 | } elsif ($webvar{action} eq 'confirm') {
|
---|
| 194 |
|
---|
[517] | 195 | $page->param(
|
---|
| 196 | cidr => $webvar{cidr},
|
---|
| 197 | custid => $webvar{custid},
|
---|
| 198 | desc => $webvar{desc},
|
---|
| 199 | disptype => $disp_alloctypes{$webvar{alloctype}}
|
---|
| 200 | );
|
---|
[199] | 201 | # Only need to check city here.
|
---|
| 202 | if ($webvar{city} eq '-') {
|
---|
[517] | 203 | $page->param(locerr => "Invalid customer location! Go back and select customer's location.");
|
---|
| 204 | goto ERRJUMP;
|
---|
[545] | 205 | }
|
---|
| 206 |
|
---|
| 207 | my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
|
---|
[199] | 208 | $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
|
---|
| 209 | $webvar{circid});
|
---|
[545] | 210 | if ($retcode eq 'OK') {
|
---|
| 211 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
|
---|
| 212 | "'$webvar{alloctype}'";
|
---|
| 213 | if ($webvar{alloctype} =~ /^.i$/) {
|
---|
| 214 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation",
|
---|
| 215 | "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n".
|
---|
| 216 | "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
|
---|
| 217 | }
|
---|
| 218 | } else {
|
---|
| 219 | $page->param(errmsg => $msg);
|
---|
| 220 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
|
---|
| 221 | "'$webvar{alloctype}' failed: '$msg'";
|
---|
| 222 | }
|
---|
[199] | 223 |
|
---|
[54] | 224 | } elsif ($webvar{action} eq 'alloctweak') {
|
---|
[517] | 225 |
|
---|
[54] | 226 | fix_allocfrom();
|
---|
| 227 | showAllocs($webvar{allocfrom});
|
---|
[517] | 228 |
|
---|
[54] | 229 | } elsif ($webvar{action} eq 'update') {
|
---|
[517] | 230 |
|
---|
[54] | 231 | update();
|
---|
[58] | 232 |
|
---|
[517] | 233 | } elsif ($webvar{action} eq 'touch') {
|
---|
[58] | 234 |
|
---|
[517] | 235 | $page->param(master => $webvar{whichmaster});
|
---|
[330] | 236 | $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
|
---|
| 237 | $sth->execute;
|
---|
| 238 | if ($sth->err) {
|
---|
[517] | 239 | $page->param(errmsg => $sth->errstr);
|
---|
[330] | 240 | }
|
---|
[517] | 241 |
|
---|
[329] | 242 | } elsif ($webvar{action} eq 'listcust') {
|
---|
[517] | 243 |
|
---|
[329] | 244 | $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
|
---|
| 245 | $sth->execute;
|
---|
[517] | 246 | my @custlist;
|
---|
[329] | 247 | while (my @data = $sth->fetchrow_array) {
|
---|
[517] | 248 | my %row = (
|
---|
| 249 | custid => $data[0],
|
---|
| 250 | custname => $data[1],
|
---|
| 251 | tech => $data[2]
|
---|
| 252 | );
|
---|
| 253 | push @custlist, \%row;
|
---|
[329] | 254 | }
|
---|
[517] | 255 | $page->param(custlist => \@custlist);
|
---|
| 256 |
|
---|
[331] | 257 | } elsif ($webvar{action} eq 'edcust') {
|
---|
[517] | 258 |
|
---|
[418] | 259 | if ($webvar{newcust}) {
|
---|
| 260 | $sth = $ip_dbh->prepare("INSERT INTO customers (custid) VALUES (?)");
|
---|
| 261 | $sth->execute($webvar{custid});
|
---|
| 262 | }
|
---|
[331] | 263 | $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
|
---|
[436] | 264 | "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ".
|
---|
[331] | 265 | "from customers where custid='$webvar{custid}'");
|
---|
| 266 | $sth->execute;
|
---|
[436] | 267 | my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) =
|
---|
[331] | 268 | $sth->fetchrow_array;
|
---|
| 269 |
|
---|
[517] | 270 | $page->param(
|
---|
| 271 | custid => $custid,
|
---|
| 272 | name => $name,
|
---|
| 273 | street => $street,
|
---|
| 274 | city => $city,
|
---|
| 275 | prov => $prov,
|
---|
| 276 | country => $country,
|
---|
| 277 | pocode => $pocode,
|
---|
| 278 | phone => $phone,
|
---|
| 279 | tech => $tech,
|
---|
| 280 | abuse => $abuse,
|
---|
| 281 | admin => $admin,
|
---|
| 282 | special => $special
|
---|
| 283 | );
|
---|
| 284 |
|
---|
[329] | 285 | } elsif ($webvar{action} eq 'updcust') {
|
---|
[517] | 286 |
|
---|
[418] | 287 | $sth = $ip_dbh->prepare("UPDATE customers SET".
|
---|
| 288 | " name=?, street=?, city=?, province=?, country=?, pocode=?,".
|
---|
| 289 | " phone=?, tech_handle=?, abuse_handle=?, admin_handle=?, special=?".
|
---|
| 290 | " WHERE custid=?");
|
---|
| 291 | $sth->execute($webvar{name}, $webvar{street}, $webvar{city}, $webvar{province},
|
---|
| 292 | $webvar{country}, $webvar{pocode}, $webvar{phone}, $webvar{tech_handle},
|
---|
| 293 | $webvar{abuse_handle}, $webvar{admin_handle}, $webvar{special}, $webvar{custid});
|
---|
[517] | 294 | $page->param(
|
---|
| 295 | custid => $webvar{custid},
|
---|
| 296 | name => $webvar{name},
|
---|
| 297 | street => $webvar{street},
|
---|
| 298 | city => $webvar{city},
|
---|
| 299 | prov => $webvar{province},
|
---|
| 300 | country => $webvar{country},
|
---|
| 301 | pocode => $webvar{pocode},
|
---|
| 302 | phone => $webvar{phone},
|
---|
| 303 | tech => $webvar{tech_handle},
|
---|
| 304 | abuse => $webvar{abuse_handle},
|
---|
| 305 | admin => $webvar{admin_handle},
|
---|
| 306 | special => $webvar{special}
|
---|
| 307 | );
|
---|
[418] | 308 |
|
---|
[65] | 309 | } elsif ($webvar{action} eq 'showpools') {
|
---|
[517] | 310 |
|
---|
| 311 | $sth = $ip_dbh->prepare("select pool, count(*) from poolips where available='y' group by pool order by pool");
|
---|
[65] | 312 | $sth->execute;
|
---|
[517] | 313 | my @poollist;
|
---|
| 314 | while (my ($pool,$free) = $sth->fetchrow_array) {
|
---|
| 315 | my %row = (
|
---|
| 316 | pool => $pool,
|
---|
| 317 | free => $free
|
---|
| 318 | );
|
---|
| 319 | push @poollist, \%row;
|
---|
[65] | 320 | }
|
---|
[517] | 321 | $page->param(poollist => \@poollist);
|
---|
| 322 |
|
---|
[65] | 323 | } elsif ($webvar{action} eq 'tweakpool') {
|
---|
[517] | 324 |
|
---|
[65] | 325 | showPool($webvar{pool});
|
---|
[517] | 326 |
|
---|
[65] | 327 | } elsif ($webvar{action} eq 'updatepool') {
|
---|
[199] | 328 |
|
---|
[65] | 329 | $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
|
---|
[517] | 330 | "city=?, type='$webvar{type}', available='".
|
---|
[65] | 331 | (($webvar{available} eq 'y') ? 'y' : 'n').
|
---|
[517] | 332 | "', notes=?, description=? ".
|
---|
[65] | 333 | "where ip='$webvar{ip}'");
|
---|
[517] | 334 | $sth->execute($webvar{city},$webvar{notes},$webvar{desc});
|
---|
| 335 | $page->param(ip => $webvar{ip});
|
---|
[65] | 336 | if ($sth->err) {
|
---|
[517] | 337 | $page->param(errmsg => $sth->errstr);
|
---|
| 338 | syslog "err", "$authuser could not update pool IP $webvar{ip}: ".$sth->errstr;
|
---|
| 339 | } else {
|
---|
[65] | 340 | syslog "notice", "$authuser updated pool IP $webvar{ip}";
|
---|
| 341 | }
|
---|
[517] | 342 | $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
|
---|
| 343 | $sth->execute;
|
---|
| 344 | my @data = $sth->fetchrow_array;
|
---|
| 345 | $page->param(pool => $data[0]);
|
---|
| 346 |
|
---|
[258] | 347 | } elsif ($webvar{action} eq 'showusers') {
|
---|
[233] | 348 |
|
---|
| 349 | $sth = $ip_dbh->prepare("select username,acl from users order by username");
|
---|
| 350 | $sth->execute;
|
---|
[517] | 351 | my @userlist;
|
---|
| 352 | while (my ($username,$acl) = $sth->fetchrow_array) {
|
---|
| 353 | ##fixme: funky things happening with HTML::Template here; shouldn't need the "logic ? iftrue : iffalse" structure
|
---|
| 354 | my %row = (
|
---|
| 355 | username => $username,
|
---|
| 356 | can_add => ($acl =~ /a/ ? 1 : 0),
|
---|
| 357 | can_change => ($acl =~ /c/ ? 1 : 0),
|
---|
| 358 | can_del => ($acl =~ /d/ ? 1 : 0),
|
---|
| 359 | sysnet => ($acl =~ /s/ ? 1 : 0),
|
---|
| 360 | is_admin => ($acl =~ /A/ ? 1 : 0),
|
---|
| 361 | acl => $acl
|
---|
| 362 | );
|
---|
| 363 | push @userlist, \%row;
|
---|
| 364 | }
|
---|
| 365 | $page->param(userlist => \@userlist);
|
---|
[233] | 366 |
|
---|
| 367 | } elsif ($webvar{action} eq 'updacl') {
|
---|
[517] | 368 |
|
---|
| 369 | $page->param(username => $webvar{username});
|
---|
[233] | 370 | my $acl = 'b';
|
---|
| 371 | if ($webvar{admin} eq 'on') {
|
---|
[284] | 372 | $acl .= "acdsA";
|
---|
[233] | 373 | } else {
|
---|
| 374 | $acl .= ($webvar{add} eq 'on' ? 'a' : '').
|
---|
| 375 | ($webvar{change} eq 'on' ? 'c' : '').
|
---|
[284] | 376 | ($webvar{del} eq 'on' ? 'd' : '').
|
---|
| 377 | ($webvar{sysnet} eq 'on' ? 's' : '');
|
---|
[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 |
|
---|
[517] | 479 | ERRJUMP: print "Content-type: text/html\n\n".$header->output;
|
---|
| 480 | print $page->output;
|
---|
[54] | 481 |
|
---|
[517] | 482 | ##fixme: make me a footer param!
|
---|
| 483 | print qq(<hr><div><a href="$IPDB::webpath/">Back</a> to main interface</div>\n);
|
---|
[54] | 484 |
|
---|
[517] | 485 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
| 486 | my $footer = HTML::Template->new(filename => "footer.tmpl");
|
---|
| 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 |
|
---|
[54] | 492 | $ip_dbh->disconnect;
|
---|
| 493 |
|
---|
| 494 | exit;
|
---|
| 495 |
|
---|
| 496 |
|
---|
[517] | 497 | # Hokay. This is a little different. We have a few specific functions here:
|
---|
| 498 | # -> Assign arbitrary subnet from arbitrary free space
|
---|
| 499 | # -> Tweak individual DB fields
|
---|
| 500 | #
|
---|
| 501 |
|
---|
| 502 |
|
---|
[54] | 503 | # Tweak allocfrom into shape.
|
---|
| 504 | sub fix_allocfrom {
|
---|
| 505 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
|
---|
| 506 | # 3-octet class C specified
|
---|
| 507 | $webvar{allocfrom} .= ".0/24";
|
---|
| 508 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
|
---|
| 509 | # 4-octet IP specified;
|
---|
| 510 | $webvar{allocfrom} .= "/24";
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 |
|
---|
[517] | 515 | # Show allocations to allow editing.
|
---|
| 516 | sub showAllocs {
|
---|
[58] | 517 |
|
---|
[517] | 518 | my $within = new NetAddr::IP $_[0];
|
---|
| 519 | $page->param(within => $within);
|
---|
[58] | 520 |
|
---|
[517] | 521 | $sth = $ip_dbh->prepare("select cidr,custid,type,city,description from allocations where cidr <<= '$within' order by cidr");
|
---|
[54] | 522 | $sth->execute;
|
---|
[517] | 523 | my @blocklist;
|
---|
| 524 | while (my ($cidr,$custid,$type,$city,$desc) = $sth->fetchrow_array) {
|
---|
| 525 | my %row = (
|
---|
| 526 | cidr => $cidr,
|
---|
| 527 | custid => $custid,
|
---|
| 528 | city => $city,
|
---|
| 529 | desc => $desc,
|
---|
| 530 | );
|
---|
[54] | 531 |
|
---|
[517] | 532 | ##fixme: don't wanna retrieve the whole type list *every time around the outer loop*
|
---|
[348] | 533 | my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
|
---|
| 534 | " where listorder < 500 and not (type like '_i') order by listorder");
|
---|
| 535 | $sth2->execute;
|
---|
[517] | 536 | my @typelist;
|
---|
| 537 | while (my ($listtype,$dispname) = $sth2->fetchrow_array) {
|
---|
| 538 | my %subrow = (
|
---|
| 539 | type => $listtype,
|
---|
| 540 | dispname => $dispname,
|
---|
| 541 | selected => ($listtype eq $type)
|
---|
| 542 | );
|
---|
| 543 | push @typelist, \%subrow;
|
---|
[348] | 544 | }
|
---|
[517] | 545 | $row{typelist} = \@typelist;
|
---|
| 546 | push @blocklist, \%row;
|
---|
[54] | 547 | }
|
---|
[517] | 548 | $page->param(blocklist => \@blocklist);
|
---|
| 549 | } # end showAllocs()
|
---|
[54] | 550 |
|
---|
| 551 |
|
---|
| 552 | # Stuff updates into DB
|
---|
| 553 | sub update {
|
---|
[517] | 554 | # Relatively simple SQL transaction here. Note that we're deliberately NOT
|
---|
| 555 | # updating notes/desc here as it's available through the main interface.
|
---|
| 556 | $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
|
---|
| 557 | "city=?,type='$webvar{alloctype}' where cidr='$webvar{block}'");
|
---|
| 558 | $sth->execute($webvar{city});
|
---|
| 559 |
|
---|
| 560 | $page->param(block => $webvar{block});
|
---|
| 561 | if ($sth->err) {
|
---|
| 562 | $page->param(updfailed => $sth->errstr);
|
---|
| 563 | syslog "err", "$authuser could not update block '$webvar{block}': '".$sth->errstr."'";
|
---|
[54] | 564 | } else {
|
---|
| 565 | syslog "notice", "$authuser updated $webvar{block}";
|
---|
| 566 | }
|
---|
| 567 | # need to get /24 that block is part of
|
---|
| 568 | my @bits = split /\./, $webvar{block};
|
---|
| 569 | $bits[3] = "0/24";
|
---|
| 570 | showAllocs((join ".", @bits));
|
---|
[517] | 571 | } # end update()
|
---|
[65] | 572 |
|
---|
| 573 |
|
---|
| 574 | # showPool()
|
---|
| 575 | # List all IPs in a pool, and allow arbitrary admin changes to each
|
---|
| 576 | # Allow changes to ALL fields
|
---|
| 577 | sub showPool($) {
|
---|
| 578 | my $pool = new NetAddr::IP $_[0];
|
---|
[348] | 579 |
|
---|
| 580 | $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
|
---|
| 581 | $sth->execute;
|
---|
[517] | 582 | my @typelist;
|
---|
| 583 | while (my ($type,$dispname) = $sth->fetchrow_array) {
|
---|
| 584 | my %row = (
|
---|
| 585 | type => $type,
|
---|
| 586 | dispname => $dispname
|
---|
| 587 | );
|
---|
| 588 | push @typelist, \%row;
|
---|
[348] | 589 | }
|
---|
[517] | 590 | $page->param(typelist => \@typelist);
|
---|
[348] | 591 |
|
---|
[517] | 592 | $sth = $ip_dbh->prepare("select ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
|
---|
[65] | 593 | $sth->execute;
|
---|
[517] | 594 | my @iplist;
|
---|
| 595 | while (my ($ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) {
|
---|
| 596 | my %row = (
|
---|
| 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 | }
|
---|