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