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