| 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.
 | 
|---|
| 5 | #
 | 
|---|
| 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 | ###
 | 
|---|
| 12 | # Copyright (C) 2004-2010 - Kris Deugau
 | 
|---|
| 13 | 
 | 
|---|
| 14 | use strict;
 | 
|---|
| 15 | use warnings;
 | 
|---|
| 16 | use CGI::Carp qw(fatalsToBrowser);
 | 
|---|
| 17 | use CGI::Simple;
 | 
|---|
| 18 | use HTML::Template;
 | 
|---|
| 19 | use DBI;
 | 
|---|
| 20 | #use POSIX qw(ceil);
 | 
|---|
| 21 | use NetAddr::IP;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | use Sys::Syslog;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | # don't remove!  required for GNU/FHS-ish install from tarball
 | 
|---|
| 26 | ##uselib##
 | 
|---|
| 27 | 
 | 
|---|
| 28 | use CustIDCK;
 | 
|---|
| 29 | use MyIPDB;
 | 
|---|
| 30 | 
 | 
|---|
| 31 | openlog "IPDB-admin","pid","$IPDB::syslog_facility";
 | 
|---|
| 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 | 
 | 
|---|
| 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;
 | 
|---|
| 58 | my @templatepath = [ "localtemplates", "templates" ];
 | 
|---|
| 59 | 
 | 
|---|
| 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) {
 | 
|---|
| 67 |   $webvar{action} = "dberr";
 | 
|---|
| 68 | } else {
 | 
|---|
| 69 |   initIPDBGlobals($ip_dbh);
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | if(!defined($webvar{action})) {
 | 
|---|
| 73 |   $webvar{action} = "main";   #shuts up the warnings.
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | # handle DB error output
 | 
|---|
| 77 | if ($webvar{action} eq 'dberr') {
 | 
|---|
| 78 |   my $page = HTML::Template->new(filename => "admin/dberr.tmpl", path => @templatepath);
 | 
|---|
| 79 |   $page->param(errmsg => $errstr);
 | 
|---|
| 80 |   print "Content-Type: text/html\n\n".$page->output;
 | 
|---|
| 81 |   exit;
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | if ($IPDBacl{$authuser} !~ /A/) {
 | 
|---|
| 85 |   my $page = HTML::Template->new(filename => "admin/aclerr.tmpl", path => @templatepath);
 | 
|---|
| 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;
 | 
|---|
| 90 |   exit;
 | 
|---|
| 91 | }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | my $header = HTML::Template->new(filename => "admin/header.tmpl", path => @templatepath);
 | 
|---|
| 94 | $header->param(mainpage => 1) if $webvar{action} eq 'main';
 | 
|---|
| 95 | $header->param(webpath => $IPDB::webpath);
 | 
|---|
| 96 | print "Content-type: text/html\n\n".$header->output;
 | 
|---|
| 97 | 
 | 
|---|
| 98 | my $page;
 | 
|---|
| 99 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/templates/admin/$webvar{action}.tmpl") {
 | 
|---|
| 100 |   $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl", path => @templatepath);
 | 
|---|
| 101 | } else {
 | 
|---|
| 102 |   $page = HTML::Template->new(filename => "admin/dunno.tmpl", path => @templatepath);
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | # handle index page
 | 
|---|
| 106 | if ($webvar{action} eq 'main') {
 | 
|---|
| 107 |   my $tlist = getTypeList($ip_dbh, 'a');
 | 
|---|
| 108 |   $tlist->[0]->{sel} = 1;
 | 
|---|
| 109 |   $page->param(typelist => $tlist);
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   my $mlist = getMasterList($ip_dbh, 'm');
 | 
|---|
| 112 |   $page->param(masterlist => $mlist);
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | ## Non-default actions.
 | 
|---|
| 116 | 
 | 
|---|
| 117 | elsif ($webvar{action} eq 'alloc') {
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   my $cidr = new NetAddr::IP $webvar{cidr};
 | 
|---|
| 120 |   if (!$cidr || "$cidr" =~ /^0/) {
 | 
|---|
| 121 |     $page->param(errmsg => "Can't allocate something that's not a netblock/ip");
 | 
|---|
| 122 |     goto ERRJUMP;
 | 
|---|
| 123 |   }
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   my $custid = $def_custids{$webvar{alloctype}};
 | 
|---|
| 126 |   if ($custid eq '') {
 | 
|---|
| 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;
 | 
|---|
| 132 |     }
 | 
|---|
| 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 |     }
 | 
|---|
| 139 |     # Type that doesn't have a default custid
 | 
|---|
| 140 |     $custid = $webvar{custid};
 | 
|---|
| 141 |   }
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   my $maskbits = $cidr->masklen;
 | 
|---|
| 144 |   my ($fbid, $fb, $fbparent);
 | 
|---|
| 145 |   ($fbid, $fb, $fbparent) = findAllocateFrom($ip_dbh, $maskbits, $webvar{alloctype}, '','',
 | 
|---|
| 146 |         (gimme => "$cidr", allowpriv => 1));
 | 
|---|
| 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$/) {
 | 
|---|
| 153 |     my $iptype = $1;
 | 
|---|
| 154 |     my $ptmp = getBlockData($ip_dbh, $fbparent);
 | 
|---|
| 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 '".
 | 
|---|
| 159 |                 $disp_alloctypes{$webvar{alloctype}}."' to match pool ".$ptmp->{block}."\n");
 | 
|---|
| 160 |         $webvar{alloctype} = $newiptype;
 | 
|---|
| 161 |       }
 | 
|---|
| 162 |     }
 | 
|---|
| 163 |     if (!$fbid) {
 | 
|---|
| 164 |       $page->param(errmsg => "Can't allocate static IP from outside a pool!!");
 | 
|---|
| 165 |       goto ERRJUMP;
 | 
|---|
| 166 |     }
 | 
|---|
| 167 |   } else {
 | 
|---|
| 168 |     if (!$fbid) {
 | 
|---|
| 169 |       $page->param(errmsg => "Can't allocate from outside a free block!!");
 | 
|---|
| 170 |       goto ERRJUMP;
 | 
|---|
| 171 |     }
 | 
|---|
| 172 |   }
 | 
|---|
| 173 | 
 | 
|---|
| 174 |   my $alloc_from = new NetAddr::IP $fb;
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   my @cities;
 | 
|---|
| 177 |   foreach my $city (@citylist) {
 | 
|---|
| 178 |      my %row = (city => $city);
 | 
|---|
| 179 |      push @cities, \%row;
 | 
|---|
| 180 |   }
 | 
|---|
| 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 |         );
 | 
|---|
| 189 | 
 | 
|---|
| 190 | } elsif ($webvar{action} eq 'confirm') {
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   $page->param(
 | 
|---|
| 193 |         cidr => $webvar{cidr},
 | 
|---|
| 194 |         custid => $webvar{custid},
 | 
|---|
| 195 |         desc => $webvar{desc},
 | 
|---|
| 196 |         disptype => $disp_alloctypes{$webvar{alloctype}}
 | 
|---|
| 197 |         );
 | 
|---|
| 198 |   # Only need to check city here.
 | 
|---|
| 199 |   if ($webvar{city} eq '-') {
 | 
|---|
| 200 |     $page->param(locerr => "Invalid customer location!  Go back and select customer's location.");
 | 
|---|
| 201 |     goto ERRJUMP;
 | 
|---|
| 202 |   }
 | 
|---|
| 203 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 |   }
 | 
|---|
| 222 | 
 | 
|---|
| 223 | } elsif ($webvar{action} eq 'alloctweak') {
 | 
|---|
| 224 | 
 | 
|---|
| 225 |   fix_allocfrom();
 | 
|---|
| 226 |   showAllocs($webvar{allocfrom});
 | 
|---|
| 227 | 
 | 
|---|
| 228 | } elsif ($webvar{action} eq 'update') {
 | 
|---|
| 229 | 
 | 
|---|
| 230 |   update();
 | 
|---|
| 231 | 
 | 
|---|
| 232 | } elsif ($webvar{action} eq 'touch') {
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   my ($code,$msg) = touchMaster($ip_dbh, $webvar{whichmaster});
 | 
|---|
| 235 |   $page->param(errmsg => $msg) if $code eq 'FAIL';
 | 
|---|
| 236 | 
 | 
|---|
| 237 | } elsif ($webvar{action} eq 'listcust') {
 | 
|---|
| 238 | 
 | 
|---|
| 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);
 | 
|---|
| 242 | 
 | 
|---|
| 243 | } elsif ($webvar{action} eq 'edcust') {
 | 
|---|
| 244 | 
 | 
|---|
| 245 |   if ($webvar{newcust}) {
 | 
|---|
| 246 |     $sth = $ip_dbh->prepare("INSERT INTO customers (custid) VALUES (?)");
 | 
|---|
| 247 |     $sth->execute($webvar{custid});
 | 
|---|
| 248 |   }
 | 
|---|
| 249 |   $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
 | 
|---|
| 250 |         "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ".
 | 
|---|
| 251 |         "from customers where custid='$webvar{custid}'");
 | 
|---|
| 252 |   $sth->execute;
 | 
|---|
| 253 |   my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) =
 | 
|---|
| 254 |         $sth->fetchrow_array;
 | 
|---|
| 255 | 
 | 
|---|
| 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 | 
 | 
|---|
| 271 | } elsif ($webvar{action} eq 'updcust') {
 | 
|---|
| 272 | 
 | 
|---|
| 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.
 | 
|---|
| 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 |         );
 | 
|---|
| 307 | 
 | 
|---|
| 308 | } elsif ($webvar{action} eq 'showpools') {
 | 
|---|
| 309 | 
 | 
|---|
| 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);
 | 
|---|
| 314 | 
 | 
|---|
| 315 | } elsif ($webvar{action} eq 'tweakpool') {
 | 
|---|
| 316 | 
 | 
|---|
| 317 |   showPool($webvar{pool});
 | 
|---|
| 318 | 
 | 
|---|
| 319 | } elsif ($webvar{action} eq 'updatepool') {
 | 
|---|
| 320 | 
 | 
|---|
| 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='".
 | 
|---|
| 324 |         (($webvar{available} eq 'y') ? 'y' : 'n').
 | 
|---|
| 325 |         "', notes=?, description=? ".
 | 
|---|
| 326 |         "WHERE id=?");
 | 
|---|
| 327 |   $sth->execute($webvar{custid}, $webvar{city}, $webvar{type}, $webvar{notes}, $webvar{desc}, $webvar{id});
 | 
|---|
| 328 |   if ($sth->err) {
 | 
|---|
| 329 |     $page->param(errmsg => $sth->errstr);
 | 
|---|
| 330 |     syslog "err", "$authuser could not update pool IP $ip: ".$sth->errstr;
 | 
|---|
| 331 |   } else {
 | 
|---|
| 332 |     syslog "notice", "$authuser updated pool IP $ip";
 | 
|---|
| 333 |   }
 | 
|---|
| 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);
 | 
|---|
| 338 | 
 | 
|---|
| 339 | } elsif ($webvar{action} eq 'showusers') {
 | 
|---|
| 340 | 
 | 
|---|
| 341 |   $sth = $ip_dbh->prepare("select username,acl from users order by username");
 | 
|---|
| 342 |   $sth->execute;
 | 
|---|
| 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 |         can_merge => ($acl =~ /m/ ? 1 : 0),
 | 
|---|
| 353 |         is_admin => ($acl =~ /A/ ? 1 : 0),
 | 
|---|
| 354 |         acl => $acl
 | 
|---|
| 355 |         );
 | 
|---|
| 356 |     push @userlist, \%row;
 | 
|---|
| 357 |   }
 | 
|---|
| 358 |   $page->param(userlist => \@userlist);
 | 
|---|
| 359 | 
 | 
|---|
| 360 | } elsif ($webvar{action} eq 'updacl') {
 | 
|---|
| 361 | 
 | 
|---|
| 362 |   $page->param(username => $webvar{username});
 | 
|---|
| 363 |   my $acl = 'b';
 | 
|---|
| 364 |   if ($webvar{admin} eq 'on') {
 | 
|---|
| 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.
 | 
|---|
| 368 |     $acl .= "acdsA";
 | 
|---|
| 369 |     $acl .= 'm' if $webvar{merge} eq 'on';
 | 
|---|
| 370 |   } else {
 | 
|---|
| 371 |     $acl .= ($webvar{add} eq 'on' ? 'a' : '').
 | 
|---|
| 372 |         ($webvar{change} eq 'on' ? 'c' : '').
 | 
|---|
| 373 |         ($webvar{del} eq 'on' ? 'd' : '').
 | 
|---|
| 374 |         ($webvar{sysnet} eq 'on' ? 's' : '').
 | 
|---|
| 375 |         ($webvar{merge} eq 'on' ? 'm' : '');
 | 
|---|
| 376 |   }
 | 
|---|
| 377 |   $page->param(acl => $acl);
 | 
|---|
| 378 | 
 | 
|---|
| 379 |   $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
 | 
|---|
| 380 |   $sth->execute;
 | 
|---|
| 381 |   $page->param(errmsg => $sth->errstr) if $sth->err;
 | 
|---|
| 382 | 
 | 
|---|
| 383 | } elsif ($webvar{action} eq 'newuser') {
 | 
|---|
| 384 | 
 | 
|---|
| 385 |   $page->param(username => $webvar{username});
 | 
|---|
| 386 |   my $cr_pass = ($webvar{preenc} ? $webvar{password} :
 | 
|---|
| 387 |         crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
 | 
|---|
| 388 |   $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
 | 
|---|
| 389 |         "('$webvar{username}','$cr_pass','b')");
 | 
|---|
| 390 |   $sth->execute;
 | 
|---|
| 391 |   $page->param(errmsg => $sth->errstr) if $sth->err;
 | 
|---|
| 392 | 
 | 
|---|
| 393 | } elsif ($webvar{action} eq 'deluser') {
 | 
|---|
| 394 | 
 | 
|---|
| 395 |   $page->param(username => $webvar{username});
 | 
|---|
| 396 |   $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
 | 
|---|
| 397 |   $sth->execute;
 | 
|---|
| 398 |   $page->param(errmsg => $sth->errstr) if $sth->err;
 | 
|---|
| 399 | 
 | 
|---|
| 400 | } elsif ($webvar{action} eq 'emailnotice') {
 | 
|---|
| 401 | 
 | 
|---|
| 402 |   $sth = $ip_dbh->prepare("SELECT action,reciplist FROM notify");
 | 
|---|
| 403 |   $sth->execute;
 | 
|---|
| 404 |   my @spamlist;
 | 
|---|
| 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);
 | 
|---|
| 408 |     my %row = (
 | 
|---|
| 409 |         action => $action_out,
 | 
|---|
| 410 |         code => $notice_code,
 | 
|---|
| 411 |         recips => $reciplist
 | 
|---|
| 412 |         );
 | 
|---|
| 413 |     push @spamlist, \%row;
 | 
|---|
| 414 |   }
 | 
|---|
| 415 |   $page->param(spamlist => \@spamlist);
 | 
|---|
| 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;
 | 
|---|
| 421 |   my @typelist;
 | 
|---|
| 422 |   while (my ($type,$disp) = $sth->fetchrow_array) {
 | 
|---|
| 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;
 | 
|---|
| 430 |   }
 | 
|---|
| 431 |   $page->param(typelist => \@typelist);
 | 
|---|
| 432 | 
 | 
|---|
| 433 | } elsif ($webvar{action} eq 'addnotice') {
 | 
|---|
| 434 | 
 | 
|---|
| 435 |   $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:';
 | 
|---|
| 436 |   if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) {
 | 
|---|
| 437 |     $page->param(cantry => 1);
 | 
|---|
| 438 |     $webvar{reciplist} =~ s/[\r\n]+/,/g;
 | 
|---|
| 439 |     $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail};
 | 
|---|
| 440 |     $page->param(reciplist => $webvar{reciplist});
 | 
|---|
| 441 |     $page->param(dispnotice => dispNoticeCode($webvar{msgaction}.$webvar{alloctype}));
 | 
|---|
| 442 |     $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)");
 | 
|---|
| 443 | ##fixme:  automagically merge reciplists iff action already exists
 | 
|---|
| 444 |     $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist});
 | 
|---|
| 445 |     $page->param(addfailed => $sth->errstr) if $sth->err;
 | 
|---|
| 446 |   }
 | 
|---|
| 447 | 
 | 
|---|
| 448 | } elsif ($webvar{action} eq 'delnotice') {
 | 
|---|
| 449 | 
 | 
|---|
| 450 |   $page->param(dispnotice => dispNoticeCode($webvar{code}.$webvar{alloctype}));
 | 
|---|
| 451 |   $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?");
 | 
|---|
| 452 |   $sth->execute($webvar{code});
 | 
|---|
| 453 |   $page->param(delfailed => $sth->errstr) if $sth->err;
 | 
|---|
| 454 | 
 | 
|---|
| 455 | } elsif ($webvar{action} eq 'ednotice') {
 | 
|---|
| 456 | 
 | 
|---|
| 457 |   $page->param(dispnotice => dispNoticeCode($webvar{code}));
 | 
|---|
| 458 |   $page->param(code => $webvar{code});
 | 
|---|
| 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;
 | 
|---|
| 463 |   $page->param(reciplist => $reciplist);
 | 
|---|
| 464 | 
 | 
|---|
| 465 | } elsif ($webvar{action} eq 'updnotice') {
 | 
|---|
| 466 | 
 | 
|---|
| 467 |   $page->param(dispnotice => dispNoticeCode($webvar{code}));
 | 
|---|
| 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});
 | 
|---|
| 471 |   $page->param(updfailed => $sth->errstr) if $sth->err;
 | 
|---|
| 472 | 
 | 
|---|
| 473 | } elsif ($webvar{action} ne '<NULL>') {
 | 
|---|
| 474 |   $page->param(dunno => $webvar{action});
 | 
|---|
| 475 | }
 | 
|---|
| 476 | 
 | 
|---|
| 477 | ERRJUMP:
 | 
|---|
| 478 | print $page->output;
 | 
|---|
| 479 | 
 | 
|---|
| 480 | ##fixme:  make me a footer param!
 | 
|---|
| 481 | print qq(<hr><div><a href="$IPDB::webpath/">Back</a> to the main IPDB</div>\n);
 | 
|---|
| 482 | 
 | 
|---|
| 483 | # We print the footer here, so we don't have to do it elsewhere.
 | 
|---|
| 484 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
 | 
|---|
| 485 | # we're already in the admin tools, no need to provide a bottom link.  maybe.
 | 
|---|
| 486 | #$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
 | 
|---|
| 487 | 
 | 
|---|
| 488 | print $footer->output;
 | 
|---|
| 489 | 
 | 
|---|
| 490 | $sth->finish if $sth;
 | 
|---|
| 491 | $ip_dbh->disconnect;
 | 
|---|
| 492 | 
 | 
|---|
| 493 | exit;
 | 
|---|
| 494 | 
 | 
|---|
| 495 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|
| 514 | # Show allocations to allow editing.
 | 
|---|
| 515 | sub showAllocs {
 | 
|---|
| 516 | 
 | 
|---|
| 517 |   my $within = new NetAddr::IP $_[0];
 | 
|---|
| 518 |   $page->param(within => $within);
 | 
|---|
| 519 | 
 | 
|---|
| 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);
 | 
|---|
| 524 |   my @blocklist;
 | 
|---|
| 525 |   while (my ($id,$vrf,$cidr,$custid,$type,$city,$desc) = $sth->fetchrow_array) {
 | 
|---|
| 526 |     my %row = (
 | 
|---|
| 527 |         id => $id,
 | 
|---|
| 528 |         cidr => $cidr,
 | 
|---|
| 529 |         custid => $custid,
 | 
|---|
| 530 |         city => $city,
 | 
|---|
| 531 |         desc => $desc,
 | 
|---|
| 532 |         );
 | 
|---|
| 533 | 
 | 
|---|
| 534 | ##fixme:  don't wanna retrieve the whole type list *every time around the outer loop*
 | 
|---|
| 535 |     my $sth2 = $ip_dbh->prepare("SELECT type,listname FROM alloctypes".
 | 
|---|
| 536 |         " WHERE listorder < 999 AND NOT (type LIKE '_i') ORDER BY listorder");
 | 
|---|
| 537 |     $sth2->execute;
 | 
|---|
| 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;
 | 
|---|
| 546 |     }
 | 
|---|
| 547 |     $row{typelist} = \@typelist;
 | 
|---|
| 548 |     push @blocklist, \%row;
 | 
|---|
| 549 |   }
 | 
|---|
| 550 |   $page->param(blocklist => \@blocklist);
 | 
|---|
| 551 | } # end showAllocs()
 | 
|---|
| 552 | 
 | 
|---|
| 553 | 
 | 
|---|
| 554 | # Stuff updates into DB
 | 
|---|
| 555 | sub update {
 | 
|---|
| 556 |   my $cidr = $ip_dbh->selectrow_array("SELECT cidr FROM allocations WHERE id=?", undef, ($webvar{block}) );
 | 
|---|
| 557 | 
 | 
|---|
| 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.
 | 
|---|
| 560 |   $ip_dbh->do("UPDATE allocations SET custid=?, city=?, type=? WHERE id=?", undef,
 | 
|---|
| 561 |         ($webvar{custid}, $webvar{city}, $webvar{alloctype}, $webvar{block}) );
 | 
|---|
| 562 | 
 | 
|---|
| 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."'";
 | 
|---|
| 567 |   } else {
 | 
|---|
| 568 |     syslog "notice", "$authuser updated $cidr";
 | 
|---|
| 569 |   }
 | 
|---|
| 570 |   # need to get /24 that block is part of
 | 
|---|
| 571 |   my @bits = split /\./, $cidr;
 | 
|---|
| 572 |   $bits[3] = "0/24";
 | 
|---|
| 573 |   showAllocs((join ".", @bits));
 | 
|---|
| 574 | } # end update()
 | 
|---|
| 575 | 
 | 
|---|
| 576 | 
 | 
|---|
| 577 | # showPool()
 | 
|---|
| 578 | # List all IPs in a pool, and allow arbitrary admin changes to each
 | 
|---|
| 579 | # Allow changes to ALL fields
 | 
|---|
| 580 | sub showPool {
 | 
|---|
| 581 |   my $pool = shift;
 | 
|---|
| 582 | 
 | 
|---|
| 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);
 | 
|---|
| 587 | 
 | 
|---|
| 588 |   $sth = $ip_dbh->prepare("SELECT id,ip,custid,city,type,available,description,notes from poolips".
 | 
|---|
| 589 |         " WHERE parent_id=? ORDER BY ip");
 | 
|---|
| 590 |   $sth->execute($pool);
 | 
|---|
| 591 |   my @iplist;
 | 
|---|
| 592 |   while (my ($id,$ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) {
 | 
|---|
| 593 |     my %row = (
 | 
|---|
| 594 |         id => $id,
 | 
|---|
| 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;
 | 
|---|
| 604 |   }
 | 
|---|
| 605 |   $page->param(iplist => \@iplist);
 | 
|---|
| 606 | } # end showPool()
 | 
|---|
| 607 | 
 | 
|---|
| 608 | 
 | 
|---|
| 609 | # interpret the notify codes
 | 
|---|
| 610 | sub dispNoticeCode {
 | 
|---|
| 611 |   my $code = shift;
 | 
|---|
| 612 |   my $action_out = '';
 | 
|---|
| 613 | 
 | 
|---|
| 614 |   if ($code =~ /^s:/) {
 | 
|---|
| 615 |     $code =~ s/^s:/Special: /;
 | 
|---|
| 616 |     return $code;
 | 
|---|
| 617 |   }
 | 
|---|
| 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?
 | 
|---|
| 633 | # What about the eleventy-billion odd combinations possible?
 | 
|---|
| 634 | # this should give an idea of the structure tho
 | 
|---|
| 635 |     if ($target eq 'a') { $action_out .= "all"; }
 | 
|---|
| 636 |     elsif ($target eq '.i') {
 | 
|---|
| 637 |       $action_out .= "all static IPs";
 | 
|---|
| 638 |     }
 | 
|---|
| 639 |     else { $action_out .= $disp_alloctypes{$target}; }
 | 
|---|
| 640 |   }
 | 
|---|
| 641 |   return $action_out;
 | 
|---|
| 642 | }
 | 
|---|