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