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